You are here

function uc_cart_add_item in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_cart/uc_cart.module \uc_cart_add_item()
  2. 7.3 uc_cart/uc_cart.module \uc_cart_add_item()

Adds an item to a user's cart.

7 calls to uc_cart_add_item()
uc_cart_links_process in uc_cart_links/uc_cart_links.module
uc_cart_login_update in uc_cart/uc_cart.module
uc_catalog_buy_it_now_form_submit in uc_product/uc_product.module
uc_product_add_to_cart_form_submit in uc_product/uc_product.module
Submit handler for uc_product_add_to_cart_form.
uc_product_kit_add_to_cart in uc_product_kit/uc_product_kit.module

... See full list

File

uc_cart/uc_cart.module, line 1929

Code

function uc_cart_add_item($nid, $qty = 1, $data = NULL, $cid = NULL, $msg = TRUE, $check_redirect = TRUE) {
  if (isset($_SESSION['cart_order'])) {
    unset($_SESSION['cart_order']);
  }
  $cid = $cid ? $cid : uc_cart_get_id();
  $node = node_load($nid);
  if (is_null($data)) {
    $data = array(
      'module' => 'uc_product',
    );
  }
  if (!isset($data['module'])) {
    $data['module'] = 'uc_product';
  }
  if (!in_array($node->type, module_invoke_all('product_types'))) {
    drupal_set_message(t('@title is not a product. Unable to add to cart.', array(
      '@title' => $node->title,
    )), 'error');
    return;
  }
  $result = module_invoke_all('add_to_cart', $nid, $qty, $data);
  if (is_array($result) && !empty($result)) {
    foreach ($result as $row) {
      if ($row['success'] === FALSE) {
        if (isset($row['message']) && !empty($row['message'])) {
          $message = $row['message'];
        }
        else {
          $message = t('Sorry, that item is not available for purchase at this time.');
        }
        if ($row['silent'] === TRUE) {
          if ($check_redirect) {
            if (isset($_GET['destination'])) {
              drupal_goto();
            }
            $redirect = variable_get('uc_add_item_redirect', 'cart');
            if ($redirect != '<none>') {
              $_SESSION['last_url'] = uc_referer_uri();
              return $redirect;
            }
            else {
              return uc_referer_uri();
            }
          }
        }
        else {
          drupal_set_message($message, 'error');
        }
        return;
      }
    }
  }
  $item = db_fetch_object(db_query("SELECT * FROM {uc_cart_products} WHERE cart_id = '%s' AND nid = %d AND data = '%s'", $cid, $node->nid, serialize($data)));

  // If the item isn't in the cart yet, add it.
  if (is_null($item) || $item === FALSE) {
    db_query("INSERT INTO {uc_cart_products} (cart_id, nid, qty, changed, data) VALUES ('%s', %d, %d, %d, '%s')", $cid, $node->nid, $qty, time(), serialize($data));
    if ($msg) {
      drupal_set_message(t('<strong>@product-title</strong> added to <a href="!url">your shopping cart</a>.', array(
        '@product-title' => $node->title,
        '!url' => url('cart'),
      )));
    }
  }
  else {

    // Update the item instead.
    if ($msg) {
      drupal_set_message(t('Your item(s) have been updated.'));
    }
    $qty += $item->qty;
    module_invoke($data['module'], 'update_cart_item', $node->nid, $data, min($qty, 999999), $cid);
  }
  cache_clear_all();
  if ($check_redirect) {
    if (isset($_GET['destination'])) {
      drupal_goto();
    }
    $redirect = variable_get('uc_add_item_redirect', 'cart');
    if ($redirect != '<none>') {
      $_SESSION['last_url'] = uc_referer_uri();
      return $redirect;
    }
    else {
      return uc_referer_uri();
    }
  }
}