So I have a custom module function that checks a cart order for a certain product type

global $user;
$order = commerce_cart_order_load($user->uid);
$wrapper = entity_metadata_wrapper('commerce_order', $order);

foreach ($wrapper->commerce_line_items as $delta => $line_item_wrapper) {                                  
  $type = $line_item_wrapper->commerce_product->value()->type;   
    //go mental

This is used within checkout to display certain list items for "checkout progress" depending on whether a product type is in the cart (i.e show the gift aid stage if there is a donation).

The issue with this is it seems that when the order is no longer within the cart, for example when we hit the payment stage, and further go on to checkout complete. So the function commerce_cart_order_load($user->uid); no longer works. How would I achieve this same functionality checking the order for product of type 'X' when the order status is updated post-shopping-cart?

I think I can use $order = commerce_order_load(); but somehow I need to get the $order_id?

1 Answer 1

$order_id = arg(1);
$orderLoad = commerce_order_load($order_id);

foreach ($orderLoad->commerce_line_items['und'] as $line) {
    $line_item = commerce_line_item_load($line['line_item_id']);
    $product_id = $line_item->commerce_product['und'][0]['product_id']; 
    $product = commerce_product_load($product_id);
    $type = $product->type;

    if($type == 'X'){
      //go mental

If anyone has any improvements to this code (to make it more efficient etc. then please comment below). Thanks.

  • I would suggest use commerce_cart_order_load function directly with an argument for current user id: $orderLoad = commerce_cart_order_load($uid) Commented Nov 25, 2013 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.