How to Modify Cart items count in WooCommerce?

In WooCommerce, you can show distinct product items in the cart. One can also display respective quantities for each distinct product item. For instance, your cart contains 2x “Product1” and 4x “Product2”. In such scenarios, the counter will display 1+1=2. It would not display 2+4=6. In other words, it will display the total distinct item quantity rather than cart quantities. So, in this article, you will learn how to modify cart items count in WooCommerce. 

Image showing how to modify cart items count in WooCommerce

The above image shows 3 distinct product items in the cart instead of showing 7 quantities. Herein, 3 indicates distinct product items quantities whereas 7 indicates cart quantities of every distinct product item. This is what WooCommerce snippet does when implemented. Read & apply the below snippet to make it work for you.

PHP Snippet: Modify Cart items count at Mini Cart Header Widget in WooCommerce 

add_filter( 'woocommerce_cart_contents_count', 'phpsof_alter_cart_contents_count', 9999, 1 );
 
function phpsof_alter_cart_contents_count( $count ) {
   $count = count( WC()->cart->get_cart() );
   return $count;
}

Where do you add this snippet?

So, this is how you can modify cart items count in WooCommerce. Also, it is easier to activate this function with this code. So, to apply this code, just add this PHP snippet at your child theme function.php file’s bottom. It should be placed before “?>”, if you have it there. Apart from this, CSS goes in your child theme style.css file. Make sure that you are editing these files in a right manner for best results.

Is this snippet still valid?

So, this is how you can learn about modify cart items count in WooCommerce. I have applied this code on the Storefront theme and WordPress friendly hosting PHP 7.3. Let me know if everything works as expected when you code. Share it further if you find this snippet useful for you and it had saved your time. 

Also Read, How to Show Product Upsells at Thank You Page in WooCommerce?

How to Create Custom Product Tabs in WooCommerce?

Important links: WooCommerce – https://woocommerce.com/