You are here

function basic_cart_add_to_cart in Basic cart 7

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

File

./basic_cart.cart.inc, line 130
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))) {
      $_SESSION['basic_cart']['cart'][$nid]->basic_cart_quantity++;
    }
    else {

      // Slower, but easyer to implement.
      $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;

      // Storing in session.
      $_SESSION['basic_cart']['cart'][$nid] = $node;
    }
  }
  drupal_goto('cart');
}