问题
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.
wc-cart-example-screenshot
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!
回答1:
To make it work everywhere with different settings based on specific products, try the following instead:
// General quantity settings
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ){
$product_ids = array(27345, 27346);
$condition = in_array( $product->get_id(), $product_ids );
if( ! is_cart() ) {
$args['input_value'] = $condition ? 25 : 26; // Starting value
}
$args['min_value'] = $condition ? 25 : 26; // Minimum value
$args['max_value'] = $condition ? 500 : 260; // Maximum value
$args['step'] = $condition ? 25 : 26; // Step value
return $args;
}
// For Ajax add to cart button (define the min and max value)
add_filter( 'woocommerce_loop_add_to_cart_args', 'custom_loop_add_to_cart_quantity_arg', 10, 2 );
function custom_loop_add_to_cart_quantity_arg( $args, $product ) {
$product_ids = array(27345, 27346);
$condition = in_array( $product->get_id(), $product_ids );
$args['quantity'] = $condition ? 25 : 26; // Min value
return $args;
}
// For product variations (define the min value)
add_filter( 'woocommerce_available_variation', 'custom_available_variation_min_qty', 10, 3);
function custom_available_variation_min_qty( $data, $product, $variation ) {
$product_ids = array(27345, 27346);
$condition = in_array( $product->get_id(), $product_ids );
$args['min_qty'] = $condition ? 25 : 26; // Min value
$args['max_qty'] = $condition ? 500 : 260; // Max value
return $data;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
回答2:
Use action "woocommerce_update_cart_action_cart_updated" and disbale or remove or replace Quantity section with readonly text.
add_action('woocommerce_update_cart_action_cart_updated','action_cart_updated_quantity', 20, 1 );
function action_cart_updated_quantity( $cart_updated_quantity ){
// apply action with Your Quantity after updating cart.
}
来源:https://stackoverflow.com/questions/65714608/woocommerce-cart-quantity-wont-change-after-cart-update