Posted on

WooCommerce: Define a Minimum Order Amount

Today we take a look at the WooCommerce Minimum Order Amount. This snippet displays an error notification on the Cart Page and an error message @ Checkout Process if the order is below a set threshold.

WooCommerce Minimum Order Value
WooCommerce Minimum Order Value

WooCommerce Snippet: Define a Minimum Order Amount and Show Error Messages


/**
 * @snippet       WooCommerce: Define Minimum Order Amount & Show Errors
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=19947
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.4.3
 */

add_action( 'woocommerce_checkout_process', 'bbloomer_wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart', 'bbloomer_wc_minimum_order_amount' );
 
function bbloomer_wc_minimum_order_amount() {
    
    $minimum = 25; // change this to your minimum order amount

    if ( WC()->cart->subtotal < $minimum ) {

        if( is_cart() ) {

            wc_print_notice( 
                sprintf( 'You must have a minimum order amount of %s to place your order. Your current order total is %s.' , wc_price( $minimum ), wc_price( WC()->cart->subtotal ) ), 'error' 
            );

        } else {

            wc_add_notice( 
                sprintf( 'You must have a minimum order amount of %s to place your order. Your current order total is %s.' , wc_price( $minimum ), wc_price( WC()->cart->subtotal ) ), 'error' 
            );

        }
    }

}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance ๐Ÿ™‚

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares ๐Ÿ™‚ Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

27 thoughts on “WooCommerce: Define a Minimum Order Amount

  1. Rodolfo,
    Really nice your tutorials! It is helping me a lot!!!
    I do have a doubt, and if I would like to add the minimum of $1500 just if any product of the category 487 is on the cart?

    1. Hello Frederico, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  2. Great script, but how do I : –
    Allow any quantity for local pickup,
    But minimum order for delivery is ยฃ30
    and then restrict sale by hiding button.
    I have tried, but the update quantity mucks things up. (ajax call)
    I can hide button via jquery, css on load etc but putting it all together with the button clicks if you change shipping methods is creating way too much code. It has to be simpler than what I’m doing!

    1. Mike, that sounds like custom work… sorry but I can’t help here ๐Ÿ™‚

  3. Rodolfo,

    Can we switch the minimum to maximum?

    $maximum= 500; // change this to your maximum order amount

    if ( WC()->cart->subtotal > $maximim ) {

    1. Of course ๐Ÿ™‚

  4. Hi Rodolfo,

    I’ve a little issue with this snippet, the message is in 3 line, it is maybe a css problem but I’m not sure how to solve it.

    additionnaly I want only display the minmum amount not the actual cart amount

    is that code correct ?

    I'm using jupiter theme.

    1. Oscar, thanks for your comment! Yes, the snippet is correct, so you’re right, you have to fix your CSS. For the other issue, you have to work on the “sprintf” call.

  5. This adds a nice warning but I can still go to the cart. Is there a way to actually stop them from placing the order?

    1. Hey MadDog ๐Ÿ™‚ Yes, you can add some PHP to “deny checkout”. Just do a search on this site for “deny checkout” snippet and you’ll possibly learn how to do that given your if>then statement ๐Ÿ™‚ Hope this helps!

  6. Hi, is there a way of excluding the delivery charge from the total?

    1. Hey O Baba, thanks for your comment! Yes, you could try with other calls that can be found in https://docs.woocommerce.com/wc-apidocs/class-WC_Cart.html. For example, get_total_ex_tax(). Let me know!

  7. *like this – FREE Delivery on orders over โ‚น499.00

    1. Hey Pandey, thanks so much for your comment! This article should help: https://businessbloomer.com/woocommerce-add-need-spend-x-get-free-shipping-cart-page/. Let me know ๐Ÿ™‚

  8. hi Rodolfo, i want to show minimum amount for free shipping on cart and check out page. How can i do this?

  9. Hi, is it possible to define a minimum order for shipping but not have this requirement if it is local pickup? Thank you

    1. Hello Grace, thanks very much for your comment! The thing is that users don’t select the shipping method until the very end (on the checkout page)… so this message could only be displayed when such selection is made, and only on the checkout (not on the cart or single product page, as we can’t “read” something tat hasn’t been selected yet). This is custom work of course, but would the above suit?

    2. Thank you for your reply. Would I be able to use the above code to display minimum only for shipping and not require it for pickup on the checkout page?

    3. Well, yes the code would be a good start, but obviously some additional PHP needs to be put in place to detect the shipping and enable/disable the minimum order amount. Happy coding ๐Ÿ™‚

  10. Hi,
    I need something similar to this, but I need to restrict a particular product to a minimum order quantity of 25. Could this be done?

    1. Thanks for your comment Andy ๐Ÿ™‚ Yes, of course, you can just run this PHP function only for a certain product ID – take a look at this guide and see if you find the right conditional check: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/

  11. Hi very good, thank you… but I need something more… How do I make that text

    sprintf( 'You must have a minimum order amount of %s to place your order. Your current order total is %s.' ,

    be translated with wpml or “loco translator”

    Please, can you help me… thank you again

    1. Hey Juan, thanks a million for your comment! This is actually very simple, just check this example with sprintf: https://codex.wordpress.org/I18n_for_WordPress_Developers/#HTML. Let me know ๐Ÿ™‚

  12. Hello
    Can you tell me where I insert this code?
    Sorry for my Google english ๐Ÿ™‚

    1. Sanai, no problem at all! You can place this in your child theme’s functions.php file – if you need more guidance, please take a look at this video tutorial: “How to Customize WooCommerce with PHP Snippets (even if you don’t know PHP)“. Hope this helps!

  13. One suggestion: Execute the sprintf() call before the is_cart() check and then use that string in the wc_print_notice() and wc_add_notice() calls. If you ever need to change the string you only have to change it in one place.

    1. Awesome, thanks a lot Daymobrew ๐Ÿ™‚ Your input is much appreciated!

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.