uc_cart_add_item

  1. drupal
    1. 6 uc_cart.module function
    2. 7 uc_cart.module function
Drupal 7 uc_cart_add_item($nid, $qty = 1, $data = NULL, $cid = NULL, $msg = TRUE, $check_redirect = TRUE, $rebuild = TRUE)

Adds an item to a user's cart.

7 calls to uc_cart_add_item()

File

sites/all/modules/ubercart/uc_cart/uc_cart.module, line 1112
Handles all things concerning Ubercart's shopping cart.

Code

function uc_cart_add_item($nid, $qty = 1, $data = NULL, $cid = NULL, $msg = TRUE, $check_redirect = TRUE, $rebuild = TRUE) {
  $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 (!uc_product_is_product($node->type)) {
    drupal_set_message(t('@title is not a product. Unable to add to cart.', array('@title' => $node->title)), 'error');
    return;
  }

  $result = module_invoke_all('uc_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 (isset($row['silent']) && ($row['silent'] === TRUE)) {
          if ($check_redirect) {
            if (isset($_GET['destination'])) {
              drupal_goto();
            }
            $_SESSION['uc_cart_last_url'] = current_path();
            $redirect = variable_get('uc_add_item_redirect', 'cart');
            if ($redirect != '<none>') {
              return $redirect;
            }
            else {
              return current_path();
            }
          }
        }
        else {
          drupal_set_message($message, 'error');
        }
        return current_path();
      }
    }
  }

  $item = db_query("SELECT * FROM {uc_cart_products} WHERE cart_id = :cart_id AND nid = :nid AND data = :data", array(':cart_id' => $cid, ':nid' => $node->nid, ':data' => serialize($data)))->fetchObject();

  // If the item isn't in the cart yet, add it.
  if (is_null($item) || $item === FALSE) {
    db_insert('uc_cart_products')
      ->fields(array(
      'cart_id' => $cid, 
      'nid' => $node->nid, 
      'qty' => $qty, 
      'changed' => REQUEST_TIME, 
      'data' => serialize($data),
    ))
      ->execute();
    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'], 'uc_update_cart_item', $node->nid, $data, min($qty, 999999), $cid);
  }

  // If specified, rebuild the cached cart items array.
  if ($rebuild) {
    uc_cart_get_contents($cid, 'rebuild');
  }

  if ($check_redirect) {
    if (isset($_GET['destination'])) {
      drupal_goto();
    }

    $_SESSION['uc_cart_last_url'] = current_path();
    $redirect = variable_get('uc_add_item_redirect', 'cart');
    if ($redirect != '<none>') {
      return $redirect;
    }
    else {
      return current_path();
    }
  }
}