Get Number of Items in Cart
Let’s skip the introduction part and get straight to the solution. There are actually two ways depending on what you are going to achieve:
WC()->cart->get_cart_contents_count()
– it allows to get a number of cart items including their quantity counts. For example, if you have 10 snowboards and 1 avocado toast in the cart, this method will return11
! You can see it yourself if you open this method in WooCommerce source code. It just gets the whole cart array fromWC()->cart->get_cart()
and then for each item it gets its quantity parameter.count( WC()->cart->get_cart() )
– this implementation actually returns the number of unique products in the cart, one per product.
Some examples
Let’s imagine, that we have cart contents like on the screenshot:

Cart Total: <?php echo WC()->cart->get_cart_contents_count() ?>
<!-- Cart Total: 5 -->
Cart Total: <?php echo count( WC()->cart->get_cart() ) ?>
<!-- Cart Total: 2 -->
Sometimes you may find an implementation with $woocommerce
variable.
global $woocommerce;
echo 'Cart Total: ' . $woocommerce->cart->get_cart_contents_count();
global $woocommerce;
echo 'Cart Total: ' . count( $woocommerce->cart->get_cart() );
I can say you more, you may also find a recommendation to get cart items in the loop $count = 0; foreach( WC()->cart->get_cart() ...
, but what is the point of doing the loop when you have count()
function in PHP. Maybe just in case you would like to exclude a certain product from the total count.
Count every product just once using get_cart_contents_count()
Okay, what to do if you already have a ready WooCommerce theme and get_cart_contents_count()
is already used everywhere and you don’t 100 products to be shown in the cart count when customers just added 100 the same ice-creams to it.
Should you manually replace every piece of code with get_cart_contents_count()
method with count( WC()->cart->get_cart() )
. Luckily, no need to do that.
“There is a hook for that!”
add_filter( 'woocommerce_cart_contents_count', 'misha_get_cart_count' );
function misha_get_cart_count( $count ) {
return count( WC()->cart->get_cart() ); // easy!
}
Excluding a specific product from the cart count
When mentioning foreach( WC()->cart->get_cart() ...
above, I just realised that it is possible to do. So let’s look on how to do it.
$count = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// just skip the loop iteration if it is a specific product_id
if( 554 === $cart_item[ 'product_id' ] ) {
continue;
}
$count++;
}
echo $count;
Using plural form depending on the number of products in the cart
Okay, let’s say that on your website you may want to display it not like “Cart Total: 5”, but something like “5 products – $1,000,000”. I will show you how to format this. If there would be just 1 product in the cart, is will be shown as “1 product – $200,000”.
If you’re going to suggest me if-else solution, forget about that! Why doing that stuff when we have _n()
function at our service. WC()->cart->get_total()
will display the amount. Ok, great now we’ve also learnt how to get cart total!
$count = count( WC()->cart->get_cart() );
echo sprintf( _n( '%d product', '%d products', $count ), $count ) . ' – ' . WC()->cart->get_total();
// 5 products – $1,000,000
Text domain parameter is skipped here, because I am not going to translate it.
How to update cart counts when products are added via AJAX
Last but not least, AJAX. If you go to WooCommerce > Settings > Products, you will find a checkbox there:

When it is checked, the products will be added to the cart asynchronously, without page refresh. But if there is no page refresh, how can we update the cart items count? Wouldn’t it be weird, if a customer keeps adding the products to the cart, but it still shows “Cart is empty”?
WooCommerce give us a way to handle it with cart fragments, specifically with this very magiс hook woocommerce_add_to_cart_fragments
.
It works the following way – when a product has been added to the cart via AJAX, it refreshes all the elements, specified in the hook.
First of all you just have to display the number of items in your theme template as usual. And you can do it in multiple places. Just don’t forget to add a unique CSS class or ID to the element.
<a href="<?php echo wc_get_cart_url() ?>" class="misha-cart">Cart (<?php echo WC()->cart->get_cart_contents_count() ?>)</a>
Then paste the following code to your theme functions.php
(you know that I mean a child theme or a custom plugin, but current theme is also ok if you are not going to update it).
add_filter( 'woocommerce_add_to_cart_fragments', 'misha_add_to_cart_fragment' );
function misha_add_to_cart_fragment( $fragments ) {
$fragments[ '.misha-cart' ] = '<a href="' . wc_get_cart_url() . '" class="misha-cart">Cart (' . WC()->cart->get_cart_contents_count() . ')</a>';
return $fragments;
}
Line 5 does the work. So we’re adding an array element, using a CSS class or ID as a key. The array element contains the same dynamically generated HTML code which we inserted in the template a little above. Every time a product is added to the cart, WooCommerce will get the actual HTML from here.
Escaping output
What about escaping here? Do we need it for WC()->cart->get_cart_contents_count()
? As a mentioned before, this method has a filter hook in it, so we can not fully trust its output and it is better to use it that way absint( WC()->cart->get_cart_contents_count() )
;
Updated code:
$fragments[ '.misha-cart' ] = '<a href="' . esc_url( wc_get_cart_url() ) . '" class="misha-cart">Cart (' . absint( WC()->cart->get_cart_contents_count() ) . ')</a>';
I also recommend you my another tutorial on how to add products to cart programmatically.

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
echo $woocommerce->cart->cart_contents_count;
didn’t work for me, but this did:
echo wc()->cart->get_cart_contents_count();
global $woocommerce;
You are my hero…seriously, I have stumbled upon your blog numerous times while being stuck and you have provided the answer – THANK YOU!
😊🙏🏼
This worked. Thank you. Thank you. Thank you. Thank you. 😊
Great, always welcome! 😼
Thank you for the complete explanation ! Nice gift.
You’re always welcome! ;)
That worked like a charm. Thx so much for your code and explanations
you’re welcome 😌🙏🏼
Amazing. Thanks for the help!
Hi Misha.
Thanks for this post. It helped me a lot. However, since updating to woo 7.8.0 (released June 2023), fragments are now disabled by default. A customised item count I have on numerous websites broke as a result. Eventually I discovered this was due to the above update and that you now need to enable it for woocommerce_add_to_cart_fragments filters to take effect.
I just thought I’d share this in case any of your site visitors encounter similar problems. Simply paste this in your functions.php:
wp_enqueue_script( 'wc-cart-fragments' );
Long term, though, I’m wondering – as many folks see fragments as the cause of performance issues – if there’s a new/better way to display the cart item count and keep it accurate as customers add and remove items (ajax).
Regards.
Hello Conor,
Thanks for your finding. This way i was able to fix my shop with its 7.8 update.
Cheers!
Lukas