WooCommerce - the total number of products in the basket - not their quantity

I have a total basket, but I need the number of products available in the basket. I do not want to show the total quantity, but I want to show the total number of goods / orders in the basket.

Please, help!

+6
source share
2 answers

I had the same problem in a client project @ jivith.com

But I decided ...

Using the minicart / cart function replaces the total number of products in the basket - not their quantity.

$_cartQty = count( WC()->cart->get_cart() ); **or** use sizeof (WC()->cart->get_cart()); 

I get the total number of unique final products in the basket, and not an element of their quantity ...

My demo code:

 <span class="cart-items"><?php echo ($minicart_type == 'minicart-inline') ? '<span class="mobile-hide">' . sprintf( _n( '%d item', '%d items', $_cartQty, 'porto' ), $_cartQty ) . '</span><span class="mobile-show">' . $_cartQty . '</span>' : (($_cartQty > 0) ? $_cartQty : '0'); ?></span> 
+4
source

You can get the total amount of a unique product using WC()->cart->cart_contents . It contains an array of basket items. You can use the array_unique () function to avoid duplicate identifiers. So you can use array_count to get the number of unique products.

+2
source

All Articles