You are here

function uc_cart_get_contents in Ubercart 7.3

Same name and namespace in other branches
  1. 5 uc_cart/uc_cart.module \uc_cart_get_contents()
  2. 6.2 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

array An array of cart items.

16 calls to uc_cart_get_contents()
hook_uc_update_cart_item in uc_cart/uc_cart.api.php
Handles requests to update a cart item.
UbercartCartCheckoutTestCase::testCartApi in uc_cart/tests/uc_cart.test
Tests cart API.
uc_cart_add_item in uc_cart/uc_cart.module
Adds an item to a user's cart.
uc_cart_block_view in uc_cart/uc_cart.module
Implements hook_block_view().
uc_cart_checkout in uc_cart/uc_cart.pages.inc
Displays the cart checkout page built of checkout panes from enabled modules.

... See full list

File

uc_cart/uc_cart.module, line 1019

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' || $action == 'empty') {
    unset($items[$cid]);

    // Mark the current cart order (if any) as needing to be rebuilt.  We only
    // do this if the cart is being explicitly rebuilt (i.e. after an item is
    // added, removed or altered).
    $_SESSION['uc_cart_order_rebuild'] = TRUE;
  }
  if (!isset($items[$cid])) {

    // Find all cart items associated with this cart.
    if ($action != 'empty') {
      $efq = new EntityFieldQuery();
      $result = $efq
        ->entityCondition('entity_type', 'uc_cart_item')
        ->propertyCondition('cart_id', $cid)
        ->propertyOrderBy('cart_item_id', 'ASC')
        ->execute();
      if (!empty($result['uc_cart_item'])) {
        $items[$cid] = entity_load('uc_cart_item', array_keys($result['uc_cart_item']), NULL, TRUE);

        // Create a bare order and attach it to each item as context.
        $order = new UcOrder($cid);
        foreach ($items[$cid] as $item) {
          $item->order = $order;
        }
      }
      else {
        $items[$cid] = array();
      }
    }
    else {
      $items[$cid] = array();
    }

    // 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 == 'empty' || $action == 'rebuild') && empty($items[$cid]) && isset($_SESSION['uc_cart_id']) && $_SESSION['uc_cart_id'] == $cid) {
      unset($_SESSION['uc_cart_id']);
    }
  }
  return $items[$cid];
}