Suppose someone adds 2 products to cart
- The first product consists of 3 quantities
- The following product consists of 2 quantities
The whole quantity is therefore equal to 5. However, I want to limit this to 4
I use the following for this, which seems to work for 1 particular product, but not for the combination of multiple products.
// Checking and validating when updating cart item quantities when products are added to cart
add_filter( 'woocommerce_update_cart_validation', 'only_six_items_allowed_cart_update', 10, 4 );
function only_six_items_allowed_cart_update( $passed, $cart_item_key, $values, $updated_quantity ) {
$cart_items_count = WC()->cart->get_cart_contents_count();
$original_quantity = $values['quantity'];
$total_count = $cart_items_count - $original_quantity + $updated_quantity;
if( $cart_items_count > 4 || $total_count > 4 ){
// Set to false
$passed = false;
// Display a message
wc_add_notice( __( "You can’t have more than 6 items in cart", "woocommerce" ), "error" );
}
return $passed;
}
Can someone walk me through how to do that?