Set minimum, maximum and step quantity programmatically for specific variable products in WooCommerce Set minimum, maximum and step quantity programmatically for specific variable products in WooCommerce wordpress wordpress

Set minimum, maximum and step quantity programmatically for specific variable products in WooCommerce


The woocommerce_available_variation hook has the WC_Product_Variation product object as its third parameter and not the variable product.

With the woocommerce_available_variation hook you cannot set theproduct step.

The available arguments are the following (source code of the WooCommerce /includes/class-wc-product-variable.php file @version 3.0.0):

return apply_filters(    'woocommerce_available_variation',    array(        'attributes'            => $variation->get_variation_attributes(),        'availability_html'     => wc_get_stock_html( $variation ),        'backorders_allowed'    => $variation->backorders_allowed(),        'dimensions'            => $variation->get_dimensions( false ),        'dimensions_html'       => wc_format_dimensions( $variation->get_dimensions( false ) ),        'display_price'         => wc_get_price_to_display( $variation ),        'display_regular_price' => wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) ),        'image'                 => wc_get_product_attachment_props( $variation->get_image_id() ),        'image_id'              => $variation->get_image_id(),        'is_downloadable'       => $variation->is_downloadable(),        'is_in_stock'           => $variation->is_in_stock(),        'is_purchasable'        => $variation->is_purchasable(),        'is_sold_individually'  => $variation->is_sold_individually() ? 'yes' : 'no',        'is_virtual'            => $variation->is_virtual(),        'max_qty'               => 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : '',        'min_qty'               => $variation->get_min_purchase_quantity(),        'price_html'            => $show_variation_price ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',        'sku'                   => $variation->get_sku(),        'variation_description' => wc_format_content( $variation->get_description() ),        'variation_id'          => $variation->get_id(),        'variation_is_active'   => $variation->variation_is_active(),        'variation_is_visible'  => $variation->variation_is_visible(),        'weight'                => $variation->get_weight(),        'weight_html'           => wc_format_weight( $variation->get_weight() ),    ),    $this,    $variation);

To set the step to product variations you will need to use the woocommerce_quantity_input_args hook.

If you want to set the same step to all product variations you can use this hook.

The available arguments are the following (source code of the WooCommerce /includes/wc-template-functions.php file @version 2.5.0):

$defaults = array(    'input_id'     => uniqid( 'quantity_' ),    'input_name'   => 'quantity',    'input_value'  => '1',    'classes'      => apply_filters( 'woocommerce_quantity_input_classes', array( 'input-text', 'qty', 'text' ), $product ),    'max_value'    => apply_filters( 'woocommerce_quantity_input_max', -1, $product ),    'min_value'    => apply_filters( 'woocommerce_quantity_input_min', 0, $product ),    'step'         => apply_filters( 'woocommerce_quantity_input_step', 1, $product ),    'pattern'      => apply_filters( 'woocommerce_quantity_input_pattern', has_filter( 'woocommerce_stock_amount', 'intval' ) ? '[0-9]*' : '' ),    'inputmode'    => apply_filters( 'woocommerce_quantity_input_inputmode', has_filter( 'woocommerce_stock_amount', 'intval' ) ? 'numeric' : '' ),    'product_name' => $product ? $product->get_title() : '',    'placeholder'  => apply_filters( 'woocommerce_quantity_input_placeholder', '', $product ),);

If you want to manage the product step, the minimum quantity and the maximum quantity as a custom meta of the product, see @LoicTheAztec's answer:

ANSWER

To answer your question, if you want all product variations that have the parent variable product id equal to 27525 to have the following values:

  • Min quantity: 1
  • Max quantity: 24
  • Step: 1

You can use the following functions:

It works in the product page, in the cart and in the loop.

// set the min and max quantity for each product variation (based on the variable product id)add_filter( 'woocommerce_available_variation', 'variation_quantity_input_args', 10, 3 );function variation_quantity_input_args( $args, $product, $variation ) {    // set variable product ids    $sample_product_id = array( 27525 );    // if the selected variation belongs to one of the variable product ids    // set the min and max quantity    if ( in_array( $variation->get_parent_id(), $sample_product_id ) ) {        $args['min_qty'] = 1;        $args['max_qty'] = 24;        return $args;    }    return $args;}// set the step for specific variable products (and for each product variation)add_filter( 'woocommerce_quantity_input_args', 'set_step_for_specific_variable_products', 10, 2 );function set_step_for_specific_variable_products( $args, $product ) {    // set variable product ids    $sample_product_id = array( 27525 );        // on the product page    if ( ! is_cart() ) {        // if the id of the variable product is in the array        if ( in_array( $product->get_id(), $sample_product_id ) ) {            $args['input_value'] = 1;            $args['min_value'] = 1;            $args['max_value'] = 24;            $args['step'] = 1;        }    // in the cart    } else {        // if the id of the product variation belongs to a variable product present in the array        if ( in_array( $product->get_parent_id(), $sample_product_id ) ) {            $args['min_value'] = 1;            $args['step'] = 1;        }    }    return $args;}

The code has been tested and works. Add it to your active theme's functions.php.