How to get the value of the steps quantity product added to WooCommerce cart?

571 Views Asked by At

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) );
}
0

There are 0 best solutions below