Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

WordPress 5.0 is out. Test all the great new features before upgrading your Live website. Read How!

How to Add Minimum Shipping Requirements Rule on Products in WooCommerce

Updated on July 25, 2016

4 Min Read
Reading Time: 4 minutes

Chances are that you need to set some sort of least requirements on your hosted WooCommerce store before your customers checkout. There are currently multiple methods for setting least requirement in WooCommerce; the outcomes may even be identical. But I consider the techniques below to be the appropriate methods for doing it.

Shipping Requirements Rule on Products in WooCommerce

Setting A Minimum Number Of Products Required Per Order

Another significant scenario is setting a minimum number of products that must be requested at once before permitting the client to completely pay for his request and dispatching the goods. Change “10” to whatever works best for your requirements. You need to ensure that you just run this code on the cart and checkout pages. For this we utilize is_cart() and is_checkout(), which return ‘true’ at whatever point we are on those two particular pages.

Put the following code in functions.php which located in theme folder.

// Set a minimum number of products requirement before checking out
add_action( 'woocommerce_check_cart_items', 'cw_min_num_products' );
function cw_min_num_products() {
	// Only run in the Cart or Checkout pages
	if( is_cart() || is_checkout() ) {
		global $woocommerce;

		// Set the minimum number of products before checking out
		$minimum_num_products = 10;
		// Get the Cart's total number of products
		$cart_num_products = WC()->cart->cart_contents_count;

		// Compare values and add an error is Cart's total number of products
	    // happens to be less than the minimum required before checking out.
		// Will display a message along the lines of
		if( $cart_num_products < $minimum_num_products ) {
			// Display our error message
	        wc_add_notice( sprintf( '<strong>A Minimum of %s products is required before checking out.</strong>' 
	        	. '<br />Current number of items in the cart: %s.',
	        	$minimum_num_products,
	        	$cart_num_products ),
	        'error' );
		}
	}
}

minimum product requirement

Setting a Minimum Quantity per Product

Setting a minimum amount for every product is a common necessity for WooCommerce stores, especially if you are offering wholesale. Setting a minimum amount will restrict your customer(s) from buying a particular product  in lesser amounts. For us to check for the minimum amounts, we have to loop through each and every product in the cart and check it against our minimum  quantity per product necessities set up.

To set these restrictions, you have to make an array which holds your rules/limitations inside another array. Be careful when altering this array, and ensure that all code is entered precisely in order to prevent errors and unexpected results. The format you need to use is as follows:

// Product Id and Min. Quantities per Product
// id = Product ID
// min = Minimum quantity
$product_min_qty = array( 
	array( 'id' => 27, 'min' => 8 ),
	array( 'id' => 25, 'min' => 8 ),
	array( 'id' => 23, 'min' => 8 ),
	array( 'id' => 20, 'min' => 8 ),
);

Here is where the magic happens.
// Set a minimum number of products requirement before checking out
// Set minimum quantity per product before checking out
add_action( 'woocommerce_check_cart_items', 'cw_set_min_qty_per_product' );
function cw_set_min_qty_per_product() {
	// Only run in the Cart or Checkout pages
	if( is_cart() || is_checkout() ) {	
		global $woocommerce;

		// Product Id and Min. Quantities per Product
		$product_min_qty = array( 
			array( 'id' => 27, 'min' => 8 ),
			array( 'id' => 25, 'min' => 8 ),
			array( 'id' => 23, 'min' => 8 ),
			array( 'id' => 20, 'min' => 8 ),
		);

		// Will increment
		$i = 0;
		// Will hold information about products that have not
		// met the minimum order quantity
		$bad_products = array();

		// Loop through the products in the Cart
		foreach( $woocommerce->cart->cart_contents as $product_in_cart ) {
			// Loop through our minimum order quantities per product
			foreach( $product_min_qty as $product_to_test ) {
				// If we can match the product ID to the ID set on the minimum required array
				if( $product_to_test['id'] == $product_in_cart['product_id'] ) {
					// If the quantity required is less than than the quantity in the cart now
					if( $product_in_cart['quantity'] < $product_to_test['min'] ) {
						// Get the product ID
						$bad_products[$i]['id'] = $product_in_cart['product_id'];
						// Get the Product quantity already in the cart for this product
						$bad_products[$i]['in_cart'] = $product_in_cart['quantity'];
						// Get the minimum required for this product
						$bad_products[$i]['min_req'] = $product_to_test['min'];
					}
				}
			}
			// Increment $i
			$i++;
		}

		// Time to build our error message to inform the customer
		// About the minimum quantity per order.
		if( is_array( $bad_products) && count( $bad_products ) > 1 ) {
			// Lets begin building our message
			$message = '<strong>A minimum quantity per product has not been met.</strong><br />';
			foreach( $bad_products as $bad_product ) {
				// Append to the current message
				$message .= get_the_title( $bad_product['id'] ) .' requires a minimum quantity of '
						 . $bad_product['min_req'] 
						 .'. You currently have: '. $bad_product['in_cart'] .'.<br />';
			}
			wc_add_notice( $message, 'error' );
		}
	}
}

Here is how it will look on your website:

minimum quantity requirement woocommerce

Conclusion

WooCommerce permits you to utilize actions and filters to change the ordinary checkout process. All stores are distinctive and having the capacity to set these restrictions — when required — is crucial. For us developers, who need to accomplish tasks like these, knowing how to alter shipping rules is key.

Share your opinion in the comment section. COMMENT NOW

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at owais.alam@cloudways.com

Be the first to check out WordPress 5.0

Use our Staging feature before upgrading your website to WordPress 5.0.

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.