'WooCommerce check cart items quantity for multiples of 6 starting at 12
I have a client who wishes to sell products (champagne) with the possibility of mixing, in multiples of 6 bottles from 12 bottles. I work under wordpress and woocommerce.
I managed to find a php code which allows to make multiples but I would like it to work only from 12 products (and not from 1 product).
Do you know how to modify this code to make it happen? I am too new to php to find the solution. The plugins that exist are not suitable because of the mix.
Thanks for your help.
Example : I buy 10 bottles of CHAMPAGNE1 + 5 Bottles of CHAMPAGNE2 = 15 items -> an error message say I need multiple of 6 items.
BUT if I buy 1 bottle of CHAMPAGNE1 + 1 bottle of CHAMPAGNE 2 = 2 items -> I can pay, no error message. No need multiple of 6
This is the code I found and tweaked:
add_action( "woocommerce_check_cart_items", "woocommerce_check_cart_quantities" );
function woocommerce_check_cart_quantities() {
$multiples = 6;
$total_products > 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$total_products += $values["quantity"];
}
if ( ( $total_products % $multiples ) > 0 )
wc_add_notice( sprintf( __("Les commandes se font par lot de %s bouteilles. Merci de vérifier votre panier. ", "woocommerce"), $multiples ), "error" );
}
Solution 1:[1]
Updated
There is a little mistake and something is missing in your code. Try the following instead:
add_action( "woocommerce_check_cart_items", "woocommerce_check_cart_quantities" );
function woocommerce_check_cart_quantities() {
$multiples = 6;
$total_quantity = 0;
$qty_threshold = 12;
foreach ( WC()->cart->get_cart() as => $item ) {
$total_quantity += $item["quantity"];
}
if ( $total_quantity > $qty_threshold && ( $total_quantity % $multiples ) > 0 ) {
wc_add_notice( sprintf( __("Orders are made in batches of %s bottles. Please check your cart. ", "woocommerce"), $multiples ), "error" );
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
