WooCommerce 1 min read

How to count cart items in WooCommerce programmatically?

Avatar for Ahmad Tahir By Ahmad Tahir

There are two ways to count cart items depending on what you want to achieve:

  • WC()->cart->get_cart_contents_count() – it gives you total number of cart items including their quantity counts. For example, if you have 2 shirts and 1 jeans in the cart, this method will return 3!
  • count( WC()->cart->get_cart() ) – this actually returns the number of unique products in the cart, one per product.

Sometimes you may need to call global $woocommerce variable:

global $woocommerce;
echo 'Cart Total: ' . $woocommerce->cart->get_cart_contents_count();

// Result
// Cart Total: 3
global $woocommerce;
echo 'Cart Total: ' . count( $woocommerce->cart->get_cart() );

// Result
// Cart Total: 2

Have fun coding!

Leave a Reply

Your email address will not be published.

Keep reading