'WooCommerce - Reset product quantity after validation error
I have a variable product that has variations based on a meal selection, like chicken, beef. I also have a custom field called Badge Name. I want the user to be able to add the product multiple times to the cart as a single quantity, with a different variation and/or badge name. So the cart should look like this:
| Variation | Badge Name | Quantity |
|---|---|---|
| Full Package - Chicken | Tina | 1 |
| Full Package - Chicken | Mike | 1 |
I have added a custom validation, verifying that the quantity is 1. If the quantity is changed to 2 then I get the error as expected. However, I'd like the quantity to reset to 1 but it stays at 2. I can't set the max quantity to 1 because I want the user to be able to add the product to the cart multiple times per order. I can't seem to find the correct function for changing the quantity back to 1 on the product page after a validation error. Am I over-thinking this and the answer is right in front of my nose?
Here is my validation code:
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Product id for 2021 Full Package - Online
$product_id_full = 3164;
// If passed
if ( $passed ) {
// If product is added with $quantity > 1
if ( $product_id_full == $product_id && $quantity > 1 ) {
// Set error message
$message = 'Full Package must be added to the cart individually with dinner selection and badge name for each attendee.';
wc_add_notice( __( $message, 'woocommerce' ), 'error' );
$passed = false;
}
}
return $passed;
} add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
