WooCommerce: Set a Minimum Order Amount with a Message
If you run a WooCommerce store, you might want to enforce a minimum order amount to ensure profitability or cover shipping costs. This tutorial shows you how to set a minimum order amount and display a clear message to customers when their cart total doesn’t meet the requirement.
Quick Fix
- Add a custom PHP snippet to your theme’s
functions.php
file or a site-specific plugin. - Set your desired minimum order amount in the code.
- Customize the message shown to customers if the minimum is not met.
- Test the checkout process to confirm the restriction and message appear correctly.
Why This Happens
WooCommerce by default does not restrict customers from placing orders below a certain amount. This can lead to unprofitable sales or increased shipping costs that erode margins. Setting a minimum order amount helps maintain business rules by preventing checkout until the cart total reaches the threshold.
Without this restriction, customers might place orders that are too small to process efficiently or economically. The custom code hooks into WooCommerce’s validation process to enforce this rule and provide user feedback.
Requirements
- WooCommerce installed and active on your WordPress site.
- Access to your theme’s
functions.php
file or ability to add custom PHP code via a plugin. - Basic familiarity with editing WordPress theme files or using a code snippet plugin.
Step-by-step: Set a Minimum Order Amount in WooCommerce
- Backup your site: Always create a backup before editing theme files.
- Open your theme’s
functions.php
file: Use FTP, cPanel File Manager, or the WordPress theme editor. - Add the following code snippet at the end of the file:
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// Set minimum order amount
$minimum = 50; // Change this value to your desired minimum order amount
if ( WC()->cart->total < $minimum ) {
if( is_cart() ) {
wc_print_notice(
sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to proceed to checkout.',
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
} else {
wc_add_notice(
sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to proceed to checkout.',
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
}
}
}
- Save the file.
- Test your store: Add products to your cart with a total below the minimum amount and try to proceed to checkout. You should see the error message preventing checkout.
- Adjust the
$minimum
value in the code snippet to your preferred minimum order amount.
Common Pitfalls
- Editing the wrong file: Avoid editing WooCommerce plugin files directly. Use your child theme’s
functions.php
or a custom plugin. - Not backing up: Always back up before making changes to avoid site downtime.
- Incorrect minimum value: Ensure the minimum amount is a numeric value and matches your currency format.
- Cache issues: If changes don’t appear, clear your site and browser cache.
- Theme conflicts: Some themes or plugins may override WooCommerce notices. Test with default themes if issues arise.
Works on
Environment | Compatibility |
---|---|
Web Servers | Apache, Nginx, LiteSpeed |
Hosting Panels | cPanel, Plesk, DirectAdmin |
WooCommerce Versions | WooCommerce 3.0 and above |
WordPress Versions | WordPress 5.0 and above |
FAQ
- Q: Can I set different minimum order amounts for different user roles?
- A: Yes. You can modify the code to check the user role using
current_user_can()
orwp_get_current_user()
and set different minimums accordingly. - Q: How do I change the minimum order amount currency?
- A: The code uses
wc_price()
which automatically formats the amount based on your WooCommerce currency settings. - Q: Can I display the minimum order message on the cart page as well?
- A: Yes. The provided code hooks into both the cart and checkout pages to show the message in both places.
- Q: What if I want to set a minimum quantity instead of amount?
- A: You would need to check
WC()->cart->get_cart_contents_count()
instead ofWC()->cart->total
and adjust the logic accordingly. - Q: Will this prevent orders below the minimum amount from being placed?
- A: Yes. The code adds an error notice and prevents checkout submission until the minimum order amount is met.