How to show number of items in Drupal Commerce shopping cart

you can show number of items  programmatically  in your Drupal Commerce shopping cart block.

You can use this code anywhere, be it in your custom module or theme.

<?php
global $user;
$quantity = 0;
$order = commerce_cart_order_load($user->uid);
if ($order) {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $line_items = $wrapper->commerce_line_items;
    $quantity = commerce_line_items_quantity($line_items, commerce_product_line_item_types());
    $total = commerce_line_items_total($line_items);
    $currency = commerce_currency_load($total['currency_code']);
}
print format_plural($quantity, '1 item', '@count items');

 

5 thoughts on “How to show number of items in Drupal Commerce shopping cart

  1. I was suggested this web site by my cousin. I’m not sure whether this post is written by him as nobody else know
    such detailed about my problem. You are incredible!
    Thanks!

  2. Thank you for sharing your info. I really appreciate your efforts and I will be waiting for your further write
    ups thanks once again.

  3. Wonderful blog! I found it while searching on Yahoo
    News. Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Many thanks

  4. First of all I want to say superb blog! I had a quick question that I’d like
    to ask if you don’t mind. I was curious to know how you center
    yourself and clear your head prior to writing. I’ve had a difficult time
    clearing my mind in getting my ideas out. I truly do
    enjoy writing however it just seems like the first 10 to 15 minutes tend to be wasted just trying to figure out how to begin. Any ideas or tips?
    Thanks!

  5. I to find it very bothersome to share with the facts nonetheless I am going to definitely come back again.

Leave a Reply

Your email address will not be published.