I have created the custom block, here I want to count the number of products which is presented in the cart and tried with commerce_cart_order_load function to get the user selected products.

Is there any other methods to count the number of product in cart ?

Please help me how to achieve this !


If you know the store id and order type, then you can count the number of products in the current user's cart with the following code.

    $store_id = 1;
    $order_type = 'default';
    $cart_manager = \Drupal::service('commerce_cart.cart_manager');
    $cart_provider = \Drupal::service('commerce_cart.cart_provider');
    $entity_manager = \Drupal::entityManager();
    $store = $entity_manager->getStorage('commerce_store')->load($store_id); 
    $cart = $cart_provider->getCart($order_type, $store);
    $total_items = count($cart-> getItems());

In Drupal 8, carts are special orders, related to a user (like regular orders). In a custom module, you can fetch the logged in user orders, filter them by a convenient boolean field called 'cart', sort them by creation date to get the last cart for this user. Then you can get the items count for this cart order :

in mymodule.module :

use Drupal\commerce_order\Entity\Order;

function mymodule_preprocess_[page|block|something_else](&$variables) {

  // get orders...
  $query = \Drupal::entityQuery('commerce_order')
    // ...for logged in user, could be anonymous (uid = 0)
    // ...which are carts
    ->condition('cart', 1)
    // ...last created first
    ->sort('created', 'DESC')
    // ...get only one

  // execute this : you get an array of a unique order ids (last cart)
  $cartsOrders = $query->execute();

  // I found one : this user has a cart
  if(count($cartsOrders) > 0) {

    // load the order which is the last cart for logged in user
    /* @var Order $lastCart */
    $lastCart = current(Order::loadMultiple($cartsOrders));
    // send this order items count to the template
    // ({{ cartItemsCount }} in your twig)
    $variables['cartItemsCount'] = count($lastCart->getItems());

This may contains non optimal or ugly code, this is just a starting point (but quickly tested though, seems to work). Also this need to be tested in rare case scenarios or weird situations (involving anonymous users for example). My strategy (grab the last created cart order) may be wrong too...I would be happy to have a specialist feedback on this.


I spent more time on this and had to debug a few things, here is some improvments (more Drupal Commerce 2 way) :

/* @var CurrentStoreInterface $cs */
$cs = \Drupal::service('commerce_store.current_store');
/* @var CartProviderInterface $cpi */
$cpi = \Drupal::service('commerce_cart.cart_provider');
$cart = $cpi->getCart('default', $cs->getStore());

$nbItemsInCart = $cart ? count($cart->getItems()) : 0;

Are you sure this is Drupal 8 and not Drupal 7? That function is from the Drupal 7 version of commerce_cart.module. If that's the case, you can do what the code does in the title callback for the "Shopping cart (# items)" code to count the number of items in the cart:

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

To make it more performant, we've added a cart_line_items_quantity_by_id() function in the dev version (to be included in Commerce 1.14) that avoids using the wrapper. However, you'd have to convert the $order->commerce_line_items field value into an array of line item IDs yourself first.

  • Yeah, am asking for drupal 8. Will your answer work in drupal 8 ? – Panda Nov 3 '17 at 6:41
  • I am trying to display the count of product in a block. I used the above code, its showing like PHP Fatal error: Call to undefined function Drupal\\custom_hgsi_common\\Plugin\\Block\\commerce_cart_order_load() . I do not know what else to do. Please guide me – Panda Nov 3 '17 at 9:38
  • Yeah, that function doesn't exist; it appeared you were talking about Drupal 7 based on your mention of the Drupal 7 function. The same code will not work for Drupal 8. – Ryan Szrama Nov 6 '17 at 15:59

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.