Sandesh

April 23, 2020

WooCommerce: Quantity in Multiples of X on Cart and Before Checkout

I wanted to restrict WooCommerce to only selling in set quantities of 4, 8, or 12 and on. I wrote a code, which will show a quantity error on the cart page or while processing checkout. While processing checkout, it will work exactly like checkout field validation.

The code snippet below will allow the user only to purchase set a minimum quantity 4 on in multiples of 4.

Code 1: Show quantity error while checkout

/**
 * @snippet       WooCommerce: Show quantity error while checkout
 * @author        Sandesh Jangam
 * @donate $7     https://www.paypal.me/SandeshJangam/7
 */
 
add_action( 'woocommerce_checkout_process', 'ts_minimum_order_item_count' );

function ts_minimum_order_item_count() {

	$multiples = 4;
	$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( __( 'Items can only be purchased in multiples of %s.', 'woocommerce'), $multiples ), 'error' );
	}
}

Code 2: Show quantity error on the cart page

When the user visits the cart page and quantity is not in multiples of 4, then it will show notice.

/**
 * @snippet       WooCommerce: Show quantity error on the cart page
 * @author        Sandesh Jangam
 * @donate $7     https://www.paypal.me/SandeshJangam/7
 */
 
add_action( 'woocommerce_check_cart_items', 'ts_check_cart_quantities' );

function ts_check_cart_quantities() {
  	
	if( ! is_cart() ) {
		return;
	}

	$multiples = 4;
	$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' );
	}
}

Where to add custom PHP code?

Add the above PHP code at the end of to your child theme’s functions.php. There are many ways to add custom PHP code ( custom functionality ) to WordPress / WooCommerce website. Read this article for more information

Why this code is not working?

If you face any issue with the above code, then please let me know in the comment. I will be happy to review the code, and If it is working for you, then show me some love in the comment section.

Thank you in advance…!

Join 1,000+ Subscribers

Get exclusive access to WordPress, WooCommerce tips, articles, tutorials right in your inbox.​

About the Author

Sandesh Jangam is Developer, WooCommerce Expert, and Podcaster. He is working as a WordPress developer since 2014 and helping people with this blog. He loves to travel ancient temples, chilling at the beach and reading books

Sharing is Caring!
Share on facebook
Share on twitter
Share on telegram
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Share on whatsapp

Leave a Comment

Your email address will not be published. Required fields are marked *

Recommended Articles