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 the WC_Product_Variation
product object as its third parameter and not the variable product.
With the
woocommerce_available_variation
hook you cannot set the product 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:
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:
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.