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.
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 tutoraspire.com FREE
* @author Tutor Aspire
* @testedwith WooCommerce 5
* @donate $9 https://tutoraspire.com
*/
// ------------
// 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 🙂