0

In WooCommerce, I want to limit the add to cart quantities to certain steps, depending on the product category, e. g. 5, 10, 15 etc. for all products in Category 1 and 10, 20, 30 for all products in Category B. This works without problems for the single product page, using the woocommerce_quantity_input_args filter:

add_filter( 'woocommerce_quantity_input_args', 'bs_woocommerce_quantity_input_args', 10, 2 );

function bs_woocommerce_quantity_input_args( $args, $product ) {

    if( has_term( 'category_1', 'product_cat' ) ) {
        $args['min_value'] = 10;
        $args['step'] = 10;
    } else {
        $args['min_value'] = 5;
        $args['step'] = 5;
    }

    return $args;
}

However, on the cart page, the has_term condition is never met, so all product can be added in steps of 5.

I tried to apply the same logic to items in the cart, however, this obviously returns true (or false) for all items in the cart if one item meets the condition:

if( is_cart() ) {
  foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { 
    if( has_term( 'category_1', 'product_cat', $cart_item['product_id'] ) ) {
      $args['min_value'] = 10;
      $args['step'] = 10;
    }
  }
}

Based on the above, how can I set the add to cart quantities for each item in the cart depending on its categories?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.