How to Set a Minimum Order Amount for Checkout
If you sell cheaper products in your store, and sometimes it happens when the shipping amount is more than the products cost in the order. Of course, you can send such orders, but often you have to set a minimum order amount to avoid such cases.
Fortunately for woocommerce it can be done very simply and you don’t need to install any additional plugins. Just add this code snippet to your child theme’s functions.php
First example: Set a Minimum Order Amount with Shipping Cost
//Set a minimum order amount for checkout
add_action( 'woocommerce_before_cart' , 'warp_minimum_order_amount' );
add_action( 'woocommerce_before_checkout_form' , 'warp_minimum_order_amount' );
function warp_minimum_order_amount()
{
// Set this variable to specify a minimum order value
$minimum = 1000;
if ( WC()->cart->total < $minimum )
{
if( is_cart() || is_checkout() )
{
wc_print_notice(
sprintf( esc_html__( 'Your current order total is %s — you must have an order with a minimum of %s to place your order', 'your text domain' ) ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
}
}
}
add_action( 'woocommerce_checkout_process', function()
{
// Set this variable to specify a minimum order value
$minimum = 1000;
if ( WC()->cart->total < $minimum )
{
throw new Exception( sprintf( esc_html__( 'Your current order total is %s — you must have an order with a minimum of %s to place your order', 'your text domain' ) ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
) );
}
} );
Second example: Set a Minimum Order Amount without Shipping Cost
//Set a minimum order amount for checkout
add_action( 'woocommerce_before_cart' , 'warp_minimum_order_amount' );
add_action( 'woocommerce_before_checkout_form' , 'warp_minimum_order_amount' );
function warp_minimum_order_amount()
{
// Set this variable to specify a minimum order value
$minimum = 1000;
if ( WC()->cart->subtotal < $minimum )
{
if( is_cart() || is_checkout() )
{
wc_print_notice(
sprintf( esc_html__( 'Your current order total is %s — you must have an order with a minimum of %s to place your order', 'your text domain' ) ,
wc_price( WC()->cart->subtotal ),
wc_price( $minimum )
), 'error'
);
}
}
}
add_action( 'woocommerce_checkout_process', function()
{
// Set this variable to specify a minimum order value
$minimum = 1000;
if ( WC()->cart->subtotal < $minimum )
{
throw new Exception( sprintf( esc_html__( 'Your current order total is %s — you must have an order with a minimum of %s to place your order', 'your text domain' ) ,
wc_price( WC()->cart->subtotal ),
wc_price( $minimum )
) );
}
} );
omar
| #
Hi
Thanks for this! Really helpful.
Do you know how to alter it to support currency conversion? I’m using woocommerce multi-currency atm. I’ve set the minimu at 2500 PKR, but if someone buys in USD, say $30 (which is more than 2500 PKR), it still thinks the minimum amount hasn’t been reached.
Thanks in advance!
Reply
Alex Podolyan
| #
Hello Omar,
You know this is a very good question 🙂
You just need to replace this line
with this line
Unfortunately, I do not know which plugin you use for multi-currency, but this example works fine with WPML multilingual if the multi-currency option is enabled.
Best, Alex
Reply
Jess
| #
This works perfect, Thank you
Reply
nas
| #
Do you know how we can add an exception for a certain product?
Reply
Alex Podolyan
| #
I would do something like this.
The example shows that if a product with a specific ID is added to the cart, the check will not work, but you can use this example for different situations.
Reply
Marshall Sutherin
| #
This is amazing. Is there a way it can be done without including tax? Right now if I add a 50 cent product it says the total is 55 cents.
Reply
Alex Podolyan
| #
In checking the minimum order amount, you can replace the line
WC()->cart->subtotal
to(WC()->cart->subtotal - WC()->cart->tax_total)
and just take away the amount of taxes from the amount of the orderReply
Giovanni
| #
Hi, great solution! Is there a way to show the error message (like wc_add_notice wrote in woocommerce official docs) if the excluded SKU is not present in the cart?
Reply
Alex Podolyan
| #
Hello,
If I understand your question correctly, then yes, this can be done very simply and here’s an example of how I would do it.
This code must be added to both functions.
Alex
Reply
Steve
| #
Very nice! Thank you for posting this, worked like a charm… / running WP 5.2.2 / Woo 3.6.5
Reply
Daniel
| #
How can I make this minimum for just shipping not for pick up
Reply
Alex Podolyan
| #
Hi Daniel,
I tried to get the shipping cost at this stage using these functions
wc_get_chosen_shipping_method_ids()
WC()->shipping->get_shipping_methods()
I think it should be done on the checkout page and not in the cart and using some other Woo actions.
Alex
Reply
Matt
| #
Can you do this for a specific product? I only have a couple of products on my site where I’d need this to be applied.
Reply
Alex Podolyan
| #
Hi there,
I already described the solution and showed examples of how to do this for special products in the comments list for this post. Unfortunately, I don’t know all the details of what you want to do, so you can use the examples of my code yourself and do whatever you want.
Best Alex
Reply
Fabio
| #
Hi Alex,
Thank you for this nice code! I would like to disable the checkout button if the client doesn’t meets the minimum order amount. Is that also possible?
Reply
Alex Podolyan
| #
Hello Fabio,
Yes, you can easily make the checkout button inactive, for this, you need to change the woo templates that contain these buttons ( cart and checkout pages ) and add a check. The WC()->cart->total object is also available in woo templates so that you can safely use it.
Best, Alex
Reply
Marius Fermi
| #
This is amazing! Thank you for sharing and even the additional comments have been super useful to me!
Reply