I use WooCommerce cart quantity won't change after cart update answer code to customize some product quantity arguments (as min, max and step arguments).
Now I have a variable product with product id 27525, and I want to apply another rule to this product only,
Quantity rule details: min(default) qty 1
, max qty 24
and step 1
.
I tried to use the code below, the problem is,
if no variations are selected, the default/min quantity will be 25, the rule is the same as the general quantity settings,
If I choose an available variation, the default quantity will be 24.
add_filter( 'woocommerce_available_variation', 'variation_quantity_input_args', 10, 2 );
function variation_quantity_input_args( $args, $product ) {
$sample_product_id = array(27525);
if (in_array( $product->get_id(), $sample_product_id)) {
$args['min_qty'] = 1;
$args['max_qty'] = 24;
return $args;
}
}
How to change the code and apply the rules to variable product 27525?
$args['min_qty'] = 1; //default and min qty
$args['max_qty'] = 24; // max qty
$args['step'] = 1; // Seems won't work use woocommerce_available_variation, but I want to change the step to 1
The
woocommerce_available_variation
hook has theWC_Product_Variation
product object as its third parameter and not the variable product.The available arguments are the following (source code of the WooCommerce
/includes/class-wc-product-variable.php
file @version 3.0.0):To set the step to product variations you will need to use the
woocommerce_quantity_input_args
hook.The available arguments are the following (source code of the WooCommerce
/includes/wc-template-functions.php
file @version 2.5.0):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:
You can use the following functions:
The code has been tested and works. Add it to your active theme's functions.php.