WooCommerce: Set Min Purchase Amount for Specific Product

We already studied how to set min/max WooCommerce add to cart quantity programmatically. That was an easy one. This time, I want to expand on the topic, and define a “minimum order amount on a per-product basis”.

Which, translated in plain English, would be something along the lines of “set the minimum purchase amount for product XYZ to $50”. And once we do that, I expect that the add to cart quantity does non start from 1 – instead it defaults to “$50 divided by product price”. If product price is $10, I would want to set the minimum add to cart quantity to “5” on the single product and cart pages.

Makes sense? Great – here’s how it’s done.

In this example, if I want to force $50 as minimum purchase amount, the product add to cart quantity will start from 5 and not 1.

PHP Snippet: Set Min Add to Cart Quantity for Specific Product Based on Min Amount Purchase @ WooCommerce Single Product & Cart Pages

In the example below, I’m setting a minimum purchase amount for product ID = 123 of $50. This function will need to act on the single product page and cart page. If product ID = 123 price is $9, the $min amount will be: ceil(50/9) = 6

/**
 * @snippet       Set Min Purchase Amount | WooCommerce Single Product
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 5
 * @community     https://businessbloomer.com/club/
 */

// ------------
// 1. Single Product Page

add_filter( 'woocommerce_quantity_input_min', 'bloomer_woocommerce_quantity_min_50_eur', 9999, 2 );
  
function bloomer_woocommerce_quantity_min_50_eur( $min, $product ) {  

	if ( is_product() ) {
		if ( 123 === $product->get_id() ) {
			$min = ceil( 50 / $product->get_price() );
		}

	}
	
	return $min;

}

// ------------
// 2. Cart Page

add_filter( 'woocommerce_cart_item_quantity', 'bloomer_woocommerce_quantity_min_50_eur_cart', 9999, 3 );
  
function bloomer_woocommerce_quantity_min_50_eur_cart( $product_quantity, $cart_item_key, $cart_item ) {  
	
	$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
	
	$min = 0;

	if ( 123 === $_product->get_id() ) {
		$min = ceil( 50 / $_product->get_price() );
	}
	
	$product_quantity = woocommerce_quantity_input( array(
		'input_name'   => "cart[{$cart_item_key}][qty]",
		'input_value'  => $cart_item['quantity'],
		'max_value'    => $_product->get_max_purchase_quantity(),
		'min_value'    => $min,
		'product_name' => $_product->get_name(),
	), $_product, false );
	
	return $product_quantity;

}

Is There a Plugin For That?

If you’d love to code but don’t feel 100% confident with PHP, I decided to look for a reliable plugin that achieves the same result.

In this case, I recommend the WooCommerce Quantity Manager plugin. On top of setting the minimum order value, you can also define add to cart quantity step, min, max, default value for each product and much more.

But in case you hate plugins and wish to code (or wish to try that), then keep reading πŸ™‚

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: How to Fix the “Cart is Empty” Issue
    For some reason, sometimes you add products to cart but the cart page stays empty (even if you can clearly see the cart widget has products in it for example). But don’t worry – it may just be a simple cache issue (and if you don’t know what cache is that’s no problem either) or […]
  • WooCommerce: “You Only Need $$$ to Get Free Shipping!” @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. Here’s how we show a simple message on the […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

14 thoughts on “WooCommerce: Set Min Purchase Amount for Specific Product

  1. Hi Rodolfo,

    The snippet for single product page works perfectly, however the snippet for the cart page doesnt seem to work. Any idea’s?

    1. Works for me on Storefront, 2021 and 2022 themes. Please disable plugins/theme and see if it works

  2. Hi,
    I think the ‘Set Min Purchase Amount’ script is exactly the solution I need for a problem I have with one product on my website. However, I have copied and pasted the code into a snippet plugin and changed the product ID, but can’t see anything on the product page (in preview) or the cart when I buy a size below the 50 price point.

    1. Hi Michael is your product a simple one?

  3. Thanks for the snippet. I am interested to add sell some products with increment. Dor example only sell a pack of 4 chairs. Would it be possible to add an extra field on each product backend? If I set 4 then the increment for this product will be 4.

    1. Hi Konstantinos, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. Hi Rodolfo thanks for the snippet. I want to implement this for each product, not for spesific product id. I want all seperate product to be added to chart with its minimum price 200$ .

    example: you can add minimum 200$ worth product A to cart and you can add minimum 200$ worth product B but not 100$ A and 100$ B.

    1. Hello Ismail, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  5. Shouldn’t this line …

    if ( 123 === $product->get_id() ) {

    .. instead be …

    if ( 123 === $_product->get_id() ) {

    ???

    1. YES! Thanks a million. Snippet revised

  6. Hi Rodolfo.
    Thank you for your snippet.
    But unfortunately, this snippet is not working on cart page.
    can you please revise the code and update?
    Thanks

    1. Hi there, works for me. Can you try with a default theme and no other plugins than Woo?

  7. So I guess that you prefer this method over the min-max plugin that is out there, especially if you only need it for a couple of products?
    I have the plugin installed and I’m using it. Would my installation work better if I get rid of the plugin and use this snippet instead?

    1. Yes, I guess that’s my idea re: quick and easy snippets vs complex plugins πŸ™‚

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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