You are here

function basic_cart_add_to_cart in Basic cart 7.2

Same name and namespace in other branches
  1. 7.3 basic_cart.cart.inc \basic_cart_add_to_cart()
  2. 7 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 string reference to 'basic_cart_add_to_cart'
basic_cart_menu in ./basic_cart.module
Implements hook_menu().

File

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

Code

function basic_cart_add_to_cart($nid = NULL) {
  $nid = (int) $nid;
  if ($nid > 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++;
    }
    else {
      $node = node_load($nid);
      $node->basic_cart_quantity = 1;

      // 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);
  }
}