You are here

function uc_cart_get_contents in Ubercart 6.2

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

Grabs the items in a shopping cart for a user.

Parameters

$cid: (optional) The cart ID to load, or NULL to load the current user's cart.

$action: (optional) Carts are statically cached by default. If set to "rebuild", the cache will be ignored and the cart reloaded from the database.

Return value

An array of cart items.

20 calls to uc_cart_get_contents()
hook_update_cart_item in docs/hooks.php
Handles requests to update a cart item.
theme_cart_review_table in uc_cart/uc_cart_checkout_pane.inc
Formats the cart contents table on the checkout page.
UbercartCartCheckoutTestCase::testCartAPI in uc_cart/uc_cart.test
uc_cart_add_item in uc_cart/uc_cart.module
Adds an item to a user's cart.
uc_cart_block in uc_cart/uc_cart.module
Implements hook_block().

... See full list

File

uc_cart/uc_cart.module, line 1367

Code

function uc_cart_get_contents($cid = NULL, $action = NULL) {
  static $items = array();
  $cid = $cid ? $cid : uc_cart_get_id(FALSE);

  // If we didn't get a cid, return empty.
  if (!$cid) {
    return array();
  }
  if ($action == 'rebuild') {
    unset($items[$cid]);
  }
  if (!isset($items[$cid])) {
    $items[$cid] = array();
    $result = db_query("SELECT * FROM {uc_cart_products} WHERE cart_id = '%s' ORDER BY cart_item_id ASC", $cid);
    while ($item = db_fetch_object($result)) {
      if ($item = uc_cart_get_item($item)) {
        $items[$cid][] = $item;
      }
    }

    // Allow other modules a chance to alter the fully loaded cart object.
    drupal_alter('uc_cart', $items[$cid]);

    // When there are no longer any items in the cart, the anonymous cart ID is no longer required.
    // To guard against unsetting the session ID in the middle of an uc_cart_add_item() call, we only do this on rebuild
    // See issue 858816 for details.
    if ($action == 'rebuild' && empty($items[$cid]) && isset($_SESSION['uc_cart_id']) && $_SESSION['uc_cart_id'] == $cid) {
      unset($_SESSION['uc_cart_id']);
    }
  }
  return $items[$cid];
}