Skip to content

Instantly share code, notes, and snippets.

@kloon

kloon/gist:4545677

Last active May 18, 2020
Embed
What would you like to do?
WooCommerce limit checkout to quantities in multiples of a number
<?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' );
}
?>
@ashvinrathod

This comment has been minimized.

Copy link

@ashvinrathod ashvinrathod commented Nov 24, 2013

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?

@kimikographics

This comment has been minimized.

Copy link

@kimikographics kimikographics commented Dec 20, 2017

Hi. There is no way to set multiplicity for woocommerce attributes? Not only for all products or shipping class. Thank you

@raphzoro

This comment has been minimized.

Copy link

@raphzoro raphzoro commented Mar 9, 2018

please is there a way to set users after purchasing an order, d user cant make another order after a setting period of time

@e-popotam

This comment has been minimized.

Copy link

@e-popotam e-popotam commented May 18, 2020

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