During testing I'm creating lots of carts and orders with the one user. I'm using commerce_cart_order_load($user->uid) from within hook_commerce_checkout_page_info_alter() but it's consistently returning an old (the user's first?) cart rather than the current / most recent cart for that user.

I'm using commerce_nocart to provide direct checkout for event registrations, so not sure this makes a difference.

How do programmatically get the user's current order (specifically the current order total) from within hook_commerce_checkout_page_info_alter() ?

Comments

inteja created an issue. See original summary.

czigor’s picture

Status: Active » Postponed (maintainer needs more info)

commerce_cart_order_load() should be working fine for this purpose. What does it return when running it in devel/php?

inteja’s picture

It returns an old (that user's first) abandoned cart, not the most recent. When I delete that cart and subsequently test, it returns the 2nd abandoned cart for that user, not the most recent/current.

jsacksick’s picture

@inteja:

  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();
  }

For logged in users, the one with the highest order_id should be returned first.

Could you check if you don't have a module that implements the hook_commerce_cart_order_id() ? That could be the explanation.

inteja’s picture

Looks like commerce_nocart implements hook_commerce_order_id() :

/**
 * Implements hook_commerce_cart_order_id().
 */
function commerce_nocart_commerce_cart_order_id($uid = 0) {
  return _commerce_nocart_add_to_cart_order_id($uid);
}

/**
 * Enforce or reset the order used during the add-to-cart submit callback.
 */
function _commerce_nocart_add_to_cart_order_id($uid, $order_id = NULL) {
  $stored_order_id = &drupal_static(__FUNCTION__, array());

  if (isset($order_id)) {
    $stored_order_id[$uid] = (int) $order_id;
    commerce_cart_order_ids_reset();
  }

  if (isset($stored_order_id[$uid])) {
    return $stored_order_id[$uid];
  }
}
rszrama’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)