2

I used this code to filter the products added to the cart of a certain category (id = 70).

My goal is to count the number of step quantities added to the cart of a product belonging to the '70' category.

Example:

Minimum quantity: 0

Step: 100

I added the product to the cart with 300 quantities. So the step number is 3.

Is there a way to get the number of step quantities of a product added to the cart?

// Utility function that count specific product category cart items
add_shortcode('palletcompleto', 'cat_cart_count');
function cat_cart_count( $term_ids ) {
    $quantity = 0; // Initializing

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( has_product_category( $cart_item['product_id'],  array( 70 ) ) ) {

            $quantity += $cart_item['step_quantity'];
        }
    }
    // Returning category count
    return $quantity == 0 ? false : $quantity;
}

I am using some code like has_product_category() function from this thread:
Set item quantity to multiples of “x” for products in a specific category in Woocommerce

// Custom conditional function that checks also for parent product categories
function has_product_category( $product_id, $category_ids ) {
    $term_ids = array(); // Initializing

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
        if( $term->parent > 0 ){
            $term_ids[] = $term->parent; // Set the parent product category
            $term_ids[] = $term->term_id;
        } else {
            $term_ids[] = $term->term_id;
        }
    }
    return array_intersect( $category_ids, array_unique($term_ids) );
}
Vincenzo Di Gaetano
  • 3,892
  • 3
  • 13
  • 32
  • I don't think that you can get this information from cart (or even from the product)… You could be able to get it from the product if it was accessible… the product object from cart is `$product = $cart_item['data'];` – LoicTheAztec Oct 30 '18 at 09:07
  • Consider that the minimum quantity and step quantities I added to each product through the Booster Plus for Woocommerce plugin. – Vincenzo Di Gaetano Oct 30 '18 at 09:12
  • You got the limitations of this plugin, that is closed and doesn't allow that… – LoicTheAztec Oct 30 '18 at 12:42

0 Answers0