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:

Some examples

Let’s imagine, that we have cart contents like on the screenshot:

WooCommerce amount of products in the cart
There are 4 items of one particular product and 1 item of another product in the cart.
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:

add to cart behaviour option WooCommerce

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

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

Follow me on X