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!

Categorized in:

WooCommerce,

Tagged in: