Search code examples
phpwordpresswoocommerceproduct-variationsproduct-quantity

Handle variable product custom input quantity arguments in WooCommerce


I have a store set up in WooCommerce that requires product quantity to increase in steps of 6. We set it up that way with a code that is dependant on each product slug individual slug. It works fine with single products but it breaks on variable products (adding a 7th item and then continuing to increse product quantity in steps of 6).

Here is my code:

add_filter( 'woocommerce_quantity_input_args', 'quantity_selected_number', 10, 2 );
function quantity_selected_number( $args, $product ) {
//global $product;
   if ( ! is_cart() ) {
 if ($product->get_slug()=="product-01-slug"){
      $args['input_value'] = 6; // Start from this value (default = 1) 
      $args['max_value'] = 600; // Maximum quantity (default = -1)
      $args['min_value'] = 6; // Minimum quantity (default = 0)
      $args['step'] = 6; // Increment or decrement by this value (default = 1)
 }
   } 
 if ($product->get_slug()=="product-02-slug"){
      // Cart's 'min_value' is 0
      $args['input_value'] = 3;
      $args['max_value'] = 260; 
      $args['step'] = 3; 
      $args['min_value'] = 3;
 }
if ($product->get_slug()=="product-03-slug"){
      // Cart's 'min_value' is 0
      $args['input_value'] = 3;
      $args['max_value'] = 260; 
      $args['step'] = 3; 
      $args['min_value'] = 3;
 }
   return $args; 
}

Any idea why that is and what could make it work for variable products?


Solution

  • To handle product variation for variable products, you need an additional hooked function.

    I have also added another function to handle Ajax add to cart.

    Below I have revisited your code too as there were some little mistakes.

    Try the following instead:

    add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
    function custom_quantity_input_args( $args, $product ) {
        if  ( $product->get_slug() === 'product-01-slug' ) {
            if ( ! is_cart() ) {
                $args['input_value'] = 6; // default: 1 
            }
            $args['min_value'] = 6; // default: 0
            $args['max_value'] = 600; // default: -1
            $args['step'] = 6; // default: 1
        }
    
        if ( in_array( $product->get_slug(), array('product-02-slug', 'product-03-slug') ) ) {
            if ( ! is_cart() ) {
                $args['input_value'] = 3;
            }
            $args['min_value'] = 3;
            $args['max_value'] = 260; 
            $args['step'] = 3; 
        }
    
        return $args; 
    }
    
    // For Ajax add to cart button (define the Input quantity value)
    add_filter( 'woocommerce_loop_add_to_cart_args', 'loop_ajax_add_to_cart_quantity_fix', 10, 2 );
    function loop_ajax_add_to_cart_quantity_fix( $args, $product ) {
        if ( $product->get_slug() === 'product-01-slug' ) {
            $args['quantity'] = 6; // default: 1 
        }
    
        if ( in_array( $product->get_slug(), array('product-02-slug', 'product-03-slug') ) ) {
            $args['quantity'] = 3; 
        }
    
        return $args;
    }
    
    // For product variations (define the min and max values)
    add_filter( 'woocommerce_available_variation', 'filter_available_variation_qty', 10, 3 );
    function filter_available_variation_qty( $data, $product, $variation ) { 
        if ( $product->get_slug() === 'product-01-slug' ){
            $data['min_qty'] = 6; // default: 0
            $data['max_qty'] = 600; // default: -1
        }
    
        if ( in_array( $product->get_slug(), array('product-02-slug', 'product-03-slug') ) ) {
            $data['min_qty'] = 3;
            $data['max_qty'] = 260; 
        }
    
        return $data;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    Related: Set quantity minimum, maximum and step at product level in Woocommerce