How to Count Items Added to Cart WooCommerce Cart Count Code

WooCommerce Cart Count CodeIn this post, I will share with you the WooCommerce cart count code that helps you to determine the number of products in the cart.  When you are creating a plugin or WooCommerce theme you can customize the cart and checkout options to give the users a unique experience.

One of the most common ways to customize WooCommerce is to change the functionality and appearance of the cart. You can also extend WooCommerce default cart function by adding more logical conditions using the WooCommerce cart count code I will share in this post.

Get Number of Products Added to Cart: WooCommerce Cart Count Code

A good example is to apply a discount for every product added to cart. In such a case you need to know how many products are added to the cart.

To count the number of products added to the cart you should simply use the following code:

<?php

/**
* Count number of items added to cart
*/


WC()->cart->get_cart_contents_count();

Just like the previous tutorial on how to get payment methods in WooCommerce, we are using the WC() object but in this case, we have a different method that counts the number of items in the cart – get_cart_contents_count().

You can use this code in the menu or anywhere you want to show the number of products in the cart or you can use it in your logic that is based on the number of products added to the cart.

You can display the number of products added to the cart using the code below :

<?php 

/**
  * Display the number of items added to cart  
  */ 
  
  
     echo WC()->cart->get_cart_contents_count();
	 

Similar Articles

Comments are closed.