You are here

function uc_cart_add_item in Ubercart 7.3

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

Adds an item to a user's cart.

9 calls to uc_cart_add_item()
UbercartCartCheckoutTestCase::testCartApi in uc_cart/tests/uc_cart.test
Tests cart API.
uc_cart_links_process in uc_cart_links/uc_cart_links.pages.inc
Processes a Cart Link to fiddle with the cart and redirect the user.
uc_cart_login_update in uc_cart/uc_cart.module
Updates a user's cart to include items from their anonymous session.
uc_catalog_buy_it_now_form_submit in uc_product/uc_product.module
Form submission handler for uc_catalog_buy_it_now_form().
uc_product_action_add_to_cart in uc_product/uc_product.module
Action implementation: adds a product to the cart.

... See full list

File

uc_cart/uc_cart.module, line 1128

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 array(
                current_path(),
                array(
                  'query' => drupal_get_query_parameters(),
                ),
              );
            }
          }
        }
        else {
          drupal_set_message($message, 'error');
        }
        return array(
          current_path(),
          array(
            'query' => drupal_get_query_parameters(),
          ),
        );
      }
    }
  }
  $efq = new EntityFieldQuery();
  $result = $efq
    ->entityCondition('entity_type', 'uc_cart_item')
    ->propertyCondition('cart_id', $cid)
    ->propertyCondition('nid', $node->nid)
    ->propertyCondition('data', serialize($data))
    ->execute();

  // If the item isn't in the cart yet, add it.
  if (empty($result['uc_cart_item'])) {
    $item_entity = entity_create('uc_cart_item', array(
      'cart_id' => $cid,
      'nid' => $node->nid,
      'qty' => $qty,
      'data' => $data,
    ));
    entity_save('uc_cart_item', $item_entity);
    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.'));
    }
    $item_entity = entity_load_single('uc_cart_item', current(array_keys($result['uc_cart_item'])));
    $qty += $item_entity->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 array(
        current_path(),
        array(
          'query' => drupal_get_query_parameters(),
        ),
      );
    }
  }
}