You are here

function basic_cart_add_to_cart in Basic cart 7.3

Same name and namespace in other branches
  1. 7 basic_cart.cart.inc \basic_cart_add_to_cart()
  2. 7.2 basic_cart.cart.inc \basic_cart_add_to_cart()

Callback function for cart/add/.

Parameters

int $nid: We are using the node id to store the node in the shopping cart

1 call to basic_cart_add_to_cart()
basic_cart_add_to_cart_quantity_form_submit in ./basic_cart.module
Submit callback for Add to cart with quantity form.
4 string references to 'basic_cart_add_to_cart'
basic_cart_field_formatter_settings_form in ./basic_cart.module
Implements hook_field_formatter_settings_form().
basic_cart_field_formatter_settings_summary in ./basic_cart.module
Implements hook_field_formatter_settings_summary().
basic_cart_field_formatter_view in ./basic_cart.module
Implements hook_field_formatter_view().
basic_cart_menu in ./basic_cart.module
Implements hook_menu().

File

./basic_cart.cart.inc, line 102
Basic cart shopping cart implementation functions.

Code

function basic_cart_add_to_cart($nid = NULL, $quantity = 1) {
  $nid = (int) $nid;
  $quantity = (int) $quantity;
  if ($nid > 0 && $quantity > 0) {

    // If a node is added more times, just update the quantity.
    $cart = basic_cart_get_cart();
    if (!empty($cart) && in_array($nid, array_keys($cart))) {

      // Clicked 2 times on add to cart button. Increment quantity.
      $_SESSION['basic_cart']['cart'][$nid]->basic_cart_quantity += $quantity;
    }
    else {
      $node = node_load($nid);
      $node->basic_cart_quantity = $quantity;

      // Adding description.
      $body = field_get_items('node', $node, 'body');
      $description = isset($body[0]['value']) ? check_plain(strip_tags($body[0]['value'])) : '';
      $node->basic_cart_node_description = $description;

      // Unit price.
      $unit_price = field_get_items('node', $node, 'price');
      $unit_price = isset($unit_price[0]['value']) ? check_plain(strip_tags($unit_price[0]['value'])) : 0;
      $node->basic_cart_unit_price = $unit_price;

      // Storing in session.
      $_SESSION['basic_cart']['cart'][$nid] = $node;
    }
  }
  drupal_set_message(t('Shopping cart updated.'));
  $redirect = variable_get('basic_cart_redirect_user_after_add_to_cart', FALSE);
  if (empty($redirect)) {
    drupal_goto('cart');
  }
  elseif ($redirect == '<none>') {
    $referer = $_SERVER["HTTP_REFERER"];
    drupal_goto($referer);
  }
  else {
    drupal_goto($redirect);
  }
}