I'm using menu badges to display the total number of products in the commerce kickstart shopping cart. However I cannot simple use aggregation to count the total number or products using line item ID because I need to multiply it by the quantity, and add it to the total. Since the shopping cart already displays the total number of items, I though it would have been easy to do some reverse engineering but I couldn't find it.

2 Answers 2


The developers of commerce kickstart seems to have already created a menu badge view. When I clicked the drop down menu "Display menu badges", there was already a choice to select "Commerce: Cart items count"


In menu badges module check line no 335 code

 * Drupal Commcerce cart items count callback.
 * @param array $arguments
function menu_badges_commerce_cart_item_count() {
  global $user;

  if (module_exists('commerce_cart') && $order = commerce_cart_order_load($user->uid)) {
    // Count the number of product line items on the order.
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $quantity = commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types());
    if ($quantity > 0) {
      return $quantity;
  return NULL;

You can get total quantity using below code also...

  global $user;
  $order = commerce_cart_order_load($user->uid);
  $wrapper = entity_metadata_wrapper('commerce_order', $order);
  $quantity_total = 0;
  foreach ($wrapper->commerce_line_items as $delta => $line_item_wrapper) {
    $quantity = $line_item_wrapper->quantity->value();
    $quantity_total = $quantity_total + $quantity;

Commerce modules does not provide view handler for Quantity Total.. If you are interested you can create one and share it with community.. Other wise you can use views php module to achieve same...

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.