You are here

function commerce_cart_order_id in Commerce Core 7

Returns the current cart order ID for the given user.

Parameters

$uid: The uid of the customer whose cart to load. If left 0, attempts to load an anonymous order from the session.

Return value

The requested cart order ID or FALSE if none was found.

5 calls to commerce_cart_order_id()
commerce_cart_commerce_entity_access_condition_commerce_order_alter in modules/cart/commerce_cart.module
Implements hook_commerce_entity_access_condition_commerce_order_alter().
commerce_cart_order_can_refresh in modules/cart/commerce_cart.module
Checks if a cart order should be refreshed based on the shopping cart refresh settings on the order settings form.
commerce_cart_order_load in modules/cart/commerce_cart.module
Loads the shopping cart order for the specified user.
commerce_cart_plugin_argument_default_current_cart_order_id::get_argument in modules/cart/includes/views/handlers/commerce_cart_plugin_argument_default_current_cart_order_id.inc
Return the default argument.
hook_commerce_product_calculate_sell_price_line_item_alter in modules/product_pricing/commerce_product_pricing.api.php
Allows modules to alter the product line item used for sell price calculation.
1 string reference to 'commerce_cart_order_id'
commerce_cart_order_ids_reset in modules/cart/commerce_cart.module
Resets the cached array of shopping cart orders.

File

modules/cart/commerce_cart.module, line 867
Implements the shopping cart system and add to cart features.

Code

function commerce_cart_order_id($uid = 0) {

  // Cart order IDs will be cached keyed by $uid.
  $cart_order_ids =& drupal_static(__FUNCTION__);

  // Cache the user's cart order ID if it hasn't been set already.
  if (isset($cart_order_ids[$uid])) {
    return $cart_order_ids[$uid];
  }

  // First let other modules attempt to provide a valid order ID for the given
  // uid. Instead of invoking hook_commerce_cart_order_id() directly, we invoke
  // it in each module implementing the hook and return the first valid order ID
  // returned (if any).
  foreach (module_implements('commerce_cart_order_id') as $module) {
    $order_id = module_invoke($module, 'commerce_cart_order_id', $uid);

    // If a hook said the user should not have a cart, that overrides any other
    // potentially valid order ID. Return FALSE now.
    if ($order_id === FALSE) {
      $cart_order_ids[$uid] = FALSE;
      return FALSE;
    }

    // Otherwise only return a valid order ID.
    if (!empty($order_id) && is_int($order_id)) {
      $cart_order_ids[$uid] = $order_id;
      return $order_id;
    }
  }

  // Create an array of valid shopping cart order statuses.
  $status_ids = array_keys(commerce_order_statuses(array(
    'cart' => TRUE,
  )));

  // If a customer uid was specified...
  if ($uid) {

    // Look for the user's most recent shopping cart order, although they
    // should never really have more than one.
    $cart_order_ids[$uid] = db_query('SELECT order_id FROM {commerce_order} WHERE uid = :uid AND status IN (:status_ids) ORDER BY order_id DESC', array(
      ':uid' => $uid,
      ':status_ids' => $status_ids,
    ))
      ->fetchField();
  }
  else {

    // Otherwise look for a shopping cart order ID in the session.
    if (commerce_cart_order_session_exists()) {

      // We can't trust a user's IP address to remain the same, especially since
      // it may be derived from a proxy server and not the actual client. As of
      // Commerce 1.4, this query no longer restricts order IDs based on IP
      // address, instead trusting Drupal to prevent session hijacking.
      $cart_order_ids[$uid] = db_query('SELECT order_id FROM {commerce_order} WHERE order_id IN (:order_ids) AND uid = 0 AND status IN (:status_ids) ORDER BY order_id DESC', array(
        ':order_ids' => commerce_cart_order_session_order_ids(),
        ':status_ids' => $status_ids,
      ))
        ->fetchField();
    }
    else {
      $cart_order_ids[$uid] = FALSE;
    }
  }
  return $cart_order_ids[$uid];
}