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 ?


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.

| improve this answer | |
  • You took no time in responding & it works like charm...Thanks :) – RajeevK Jun 3 '13 at 13:10
  • You can also check out the implementation in commerce_cart_menu_item_title(). – Ryan Szrama Jun 4 '13 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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