1

I have to display number of item in cart/basket in design where I can't use the menu. So my question is...how to access number of item of cart at theme level?

Is there any function for template file which can return me this, so that I can show it in page.tpl.php ?

1 Answer 1

3

You can use commerce_cart_order_load() to load the order for the current user, and an EntityMetadataWrapper to extract the relevant field data, and commerce_line_items_quantity() to get at the actual quantities. e.g.

if ($order = commerce_cart_order_load($GLOBALS['user']->uid)) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $quantity = commerce_line_items_quantity($order_wrapper->commerce_line_items);

  // Do something with $quantity...
}

This code can be used in a module or theme.

2
  • You took no time in responding & it works like charm...Thanks :)
    – RajeevK
    Commented Jun 3, 2013 at 13:10
  • You can also check out the implementation in commerce_cart_menu_item_title(). Commented Jun 4, 2013 at 18:18

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.