You are here

function basic_cart_order_node_insert in Basic cart 7.3

Implements hook_node_insert().

Adding order details and sending emails.

File

basic_cart_order/basic_cart_order.module, line 345

Code

function basic_cart_order_node_insert($node) {

  // The node is of type order.
  if ($node->type == 'order') {

    // Getting the cart.
    $cart = basic_cart_get_cart();
    if (!is_array($cart)) {
      return NULL;
    }

    // Total price.
    $price = basic_cart_get_total_price();
    $node->field_total_price[$node->language][0]['value'] = $price->total;
    $node->field_total_price[LANGUAGE_NONE][0]['value'] = $price->total;

    // VAT.
    $vat_is_enabled = (int) variable_get('basic_cart_vat_state');
    if (!empty($vat_is_enabled) && $vat_is_enabled) {
      $node->vat[$node->language][0]['value'] = $price->vat;
      $node->vat[LANGUAGE_NONE][0]['value'] = $price->vat;
    }
    field_attach_update('node', $node);
    $oid = $node->nid;
    foreach ($cart as $n) {
      $record = array(
        'oid' => $oid,
        'nid' => $n->nid,
        'quantity' => $n->basic_cart_quantity,
      );
      drupal_write_record('basic_cart_order_node', $record);
    }

    // If the basic_cart_payment module is enabled
    // we need to register the order ID to the payment ID.
    if (module_exists('basic_cart_payment')) {
      $pid = basic_cart_payment_pid();
      if (!empty($pid)) {
        db_insert('basic_cart_order_payment')
          ->fields(array(
          'oid' => $oid,
          'pid' => $pid,
        ))
          ->execute();
      }
    }

    // Seding email notifications.
    $emails = basic_cart_order_send_notifications($node);
    if ($emails >= 1) {
      basic_cart_empty_cart();

      // We will switch the view, depending on if admin or not.

      //drupal_goto('checkout/thank-you');
    }
    else {
      drupal_set_message(t('There was a problem in submitting your order. Please try again later.'), 'error');
    }
  }
}