6

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!

  • Please show how far you have done – DeshDeep Singh Aug 02 '15 at 08:50
  • hi Deshdeep, thanks for your reply. I have implemented the cart and everything. On the right top side, it appears eg. 10 items. There are 2 products having 5 quantity each in the cart. I want to show 2 orders instead of 10 items. Please suggest. thanks in advance. – wordpress tips Aug 02 '15 at 15:17

3 Answers3

4

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>
devpro
  • 16,184
  • 3
  • 27
  • 38
OpenWebWar
  • 580
  • 8
  • 16
2

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.

Domain
  • 11,562
  • 3
  • 23
  • 44
0

I things Following code will help you. I had a issue i want to show total product counts not their quantity in the cart option.

If you change on class-wc-cart.php file just change php function array_sum() to count() it will work.

public function get_cart_contents_count() {   
    return apply_filters( 'woocommerce_cart_contents_count', count( wp_list_pluck( $this->get_cart(), 'quantity' )) );
}
Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32