loading...

A Woocommerce Cart Quantity Validation Tutorial

avatar

BY Prasetyo Priambodo

{Senior Developer}

31 October 2019

Reading Time: 2 minutes

We recently undertook some site updates for a winery client of ours, who needed to have a custom quantity validation to deal with ‘minimum bottle purchase’ and how it applies to a custom mixed case. When visitors are online trying to order their wine, we don’t want to waste their time by leaving it until the checkout page, after they’ve already filled in all their details, to find out about this rule. So, this validation needs to hook onto the cart page.

The Logic Code

We have a rule that every order needs to be a minimum of 6 or multiples of 6 (Eg. 6, 12, 18, …).

function digitalnoir_minimum_order_check() {

	$cart_content = WC()->cart->cart_contents;
	$qty = 0;
	if(is_array($cart_content)){
		foreach($cart_content as $product){

			$qty += $product['quantity'];
			
		}
	}

    if (  $qty % 6 != 0 ) {
		return false;
	}
	
	return true;
		
}

Add Default Quantity Message

Ideally, this message should appear on top of the cart page, to do so we will use woocommerce_before_cart_table hook.

add_action( 'woocommerce_before_cart_table' , 'digitalnoir_minimum_order_message' );
function digitalnoir_minimum_order_message(){

	$is_valid = digitalnoir_minimum_order_check();
	if(!$is_valid){
		echo 'The order must be in multiples of 6 bottles
'; } }

Redirect if the rule isn’t met

We need to ensure that customers can’t go any further through the checkout flow if the rule isn’t met. To do so we will use the template_redirect hook.

add_action( 'template_redirect' , 'digitalnoir_minimum_order_validation_redirect' );
function digitalnoir_minimum_order_validation_redirect(){

	if(is_checkout()){
		$is_valid = digitalnoir_minimum_order_check();
		if(!$is_valid){
			wp_safe_redirect( get_permalink( get_option('woocommerce_cart_page_id') ) );
			exit;
		}
	}

}

That is a basic custom WooCommerce quantity validation without a plugin. You can extend this function to fit with your quantity validation rules. In our case, we have a ‘mixed case’ which means every 1 product is counted as 12 bottles.

To deal with this we can create a custom post type called ‘product_type’, then we will need to edit our logic code.

function digitalnoir_minimum_order_check() {

	$cart_content = WC()->cart->cart_contents;
	$qty = 0;
	if(is_array($cart_content)){
		foreach($cart_content as $product){

			$product_id = $product['product_id'];
			$is_mixed = get_post_meta($product_id, 'product_type', true);

			if($is_mixed){
				$qty += ( $product['quantity'] * 12);
			}else{
				$qty += $product['quantity'];
			}
			
		}
	}

    if (  $qty % 6 != 0 ) {
		return false;
	}
	
	return true;
		
}

WooCommerce has a bunch of hooks that make it really flexible. If you need a custom eCommerce flow to fit your business, don’t hesitate to give us a call.

Like what you see?
Subscribe now to receive regular updates

ABOUT THE AUTHOR

Prasetyo Priambodo

Pras basically has a black belt in Wordpress. He weaves our designs into beautiful code. He also has a great eye for design but is not quick to admit it!!

avatar
MORE ARTICLES LIKE THIS