Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce - Getting total products count in the cart - not their quantity

I have total cart quantity but I need count of products available in the cart. I donot want to show total quantity but want to show total products/orders in the cart.

Please help!

like image 548
wordpress tips Avatar asked Aug 02 '15 08:08

wordpress tips


2 Answers

i had same issue in client project @ jivith.com

But i resolved ...

Use in minicart / cart function replace total products count in the cart - not their quantity items

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

i getting the total unique total products count in the cart instead of item 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>
like image 101
OpenWebWar Avatar answered Oct 26 '22 02:10

OpenWebWar


You can get the total number of unique product is using WC()->cart->cart_contents. This contains an array of cart items. You can use array_unique() function to avoid repetition of the ids. So finally you can use array_count to get the count of unique products.

like image 23
Domain Avatar answered Oct 26 '22 02:10

Domain