I use the following snippet to change WooCommerce add to cart quantity rules.
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ) {
$custom_qty_product_ids = array(27345, 27346);
if (!in_array($product->get_id(), $custom_qty_product_ids)) {
$args['input_value'] = 25;
$args['max_value'] = 500;
$args['min_value'] = 25;
$args['step'] = 25;
} else {
if (in_array($product->get_id(), $custom_qty_product_ids)){
$args['input_value'] = 26;
$args['max_value'] = 260;
$args['min_value'] = 26;
$args['step'] = 26;
}
}
return $args;
}
It works on my product page, but the cart quantity won’t change after the cart update, here is the screenshot example.
The following snippet(original code snippet) works perfectly but I want to apply another rule to product 27345, 27346.
add_filter('woocommerce_quantity_input_args', 'c_qty_input_args', 10, 2);
function c_qty_input_args($args, $product) {
if(is_singular('product')) {
$args['input_value'] = 25;
}
$args['max_value'] = 500;
$args['min_value'] = 25;
$args['step'] = 25;
return $args;
}
How can I change the snippet and resolve the issue?
Thank you!
2
Answers
Use action "woocommerce_update_cart_action_cart_updated" and disbale or remove or replace Quantity section with readonly text.
To make it work everywhere with different settings based on specific products, try the following instead:
Code goes in functions.php file of the active child theme (or active theme). Tested and works.