Last active
May 18, 2020 18:15
-
-
Save kloon/4545677 to your computer and use it in GitHub Desktop.
WooCommerce limit checkout to quantities in multiples of a number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// check that cart items quantities totals are in multiples of 6 | |
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( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ), 'error' ); | |
} | |
// Limit cart items with a certain shipping class to be purchased in multiple only | |
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities_for_class' ); | |
function woocommerce_check_cart_quantities_for_class() { | |
$multiples = 6; | |
$class = 'bottle'; | |
$total_products = 0; | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
$product = get_product( $values['product_id'] ); | |
if ( $product->get_shipping_class() == $class ) { | |
$total_products += $values['quantity']; | |
} | |
} | |
if ( ( $total_products % $multiples ) > 0 ) | |
wc_add_notice( sprintf( __('You need to purchase bottles in quantities of %s', 'woocommerce'), $multiples ), 'error' ); | |
} | |
?> |
Hi. There is no way to set multiplicity for woocommerce attributes? Not only for all products or shipping class. Thank you
please is there a way to set users after purchasing an order, d user cant make another order after a setting period of time
Hi, thanks for your helpful code...
How can I modify it so that it applies to each different shipping class?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi There. I am really struggling with this and wonder if you can help me. i have placed this in the fucntions.php file but it doesn't seem to work in the checkout. It still allows me to check out with 1 product even with this code? Can you help?