3

I am trying to get the number of items in the cart just from a specific product category in WooCommerce.

I am doing a site for a winery. It has alcoholic and non-alcoholic products. All the wine falls under the main category of 'wine' or category id 34, with many subcategories and products underneath them.

For any thing that falls under this category... I need to know how many items are in the cart under this category. If there are six bottles of wine no matter if they are all the same product id's or 6 different product id's. I need to get that number of 6 from the category of 'wine' or category id of 34.

I tried this with no success.

I am very new to WooCommerce and a little new to Object Oriented.

Thanks

function cat_cart_count( $cat_name ) {

// $cat_name variable is for you normally "tshirts" or "shorts"

global $woocommerce; $cat_count = 0; 

// For each product in the cart
foreach(WC()->cart->get_cart() as $cart_item_key => $values) { 
    $_product_id = $values['product_id']; // product ID
    $_product_qty = $values['quantity']; // product quantity

    // Getting categories of the product (could be more than one)
    $terms = get_the_terms( $_product_id, 'product_cat' );

    // Checking this product has a category
    if ( $terms && ! is_wp_error( $terms ) ) { 
        $term_name = array();

        // For each category of that product
        foreach($terms as $term) {

            // Adding the category name to an array
            $term_name[] = $term->name;

            // if the product has $cat_name category
            if ( in_array( $cat_name, $term_name ) ) {

                // add 1 x product quantity to the count
                $cat_count =+ 1 * $_product_qty;
            }
        }
    }
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

3 Answers3

9

The Wordpress conditional function has_term() accepts category ID, slug, name or an array of that values.

So that is the shorter and easy way to do it. Your code will be much more compact and lighter. And you can use directly your category ID 34.

Here is your function:

function cat_cart_count( $cat_name ) {
    $cat_count = 0; 
    
    // Iterating through each cart item
    foreach(WC()->cart->get_cart() as $cart_item)  
        if( has_term( $cat_name, 'product_cat', $cart_item['product_id']))
            $cat_count += $cart_item['quantity'];
            
    return  $cat_count;
}

This code is tested and fully functional.

Usage of your function using for example echo to display the value for 34 category ID:

echo cat_cart_count( 34 );
Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
0

try this I copied this code from internet and made updates to suit your case

function check_product_in_cart() {
        global $woocommerce;
            //  id of targeted category is 5 for example

        $product_count = 0;

        // start of the loop that fetches the cart items

        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            $terms = get_the_terms( $_product->id, 'product_cat' );

            // second level loop search, in case some items have several categories
            foreach ($terms as $term) {
                $_categoryid = $term->term_id;
                if (( $_categoryid === 5 ) {

                    $product_count=+ 1 * $_product_qty; ;
                }
            }
        }

        return $product_count;
   }
Yahya Hussein
  • 8,767
  • 15
  • 58
  • 114
  • Thanks for responding. Does the category id need to go in the parentheses of this check_product_in_cart()? – Dusty Satterlee Dec 07 '16 at 20:04
  • if you want to make it dynamic just declare method like this check_product_in_cart($cat_id) and replace number 5 that is inside the method with $cat_id. and write check_product_in_cart(5) where you need to use the method. – Yahya Hussein Dec 07 '16 at 20:11
0

Use this code

function cat_cart_count( $cat_name ) {
    $cat_count = 0; 
    
    // Iterating through each cart item
    foreach(WC()->cart->get_cart() as $cart_item)  
        if( has_term( $cat_name, 'product_cat', $cart_item['product_id']))
            $cat_count += $cart_item['quantity'];
            
    return  $cat_count;
}
echo cat_cart_count('all-friends');
David Buck
  • 3,752
  • 35
  • 31
  • 35