Minimum Order Amount on WooCommerce (with Multiple Currencies)
Running an e-commerce store on WooCommerce often requires setting certain rules and guidelines to ensure a smooth checkout process for your customers. One common requirement is to set a minimum order amount, which ensures that customers meet a certain spending threshold before they can complete their purchase.
In a WooCommerce store that supports multiple currencies, the minimum order amount may need to be adjusted based on the customer's chosen currency. This is where the code you provided comes in handy. Let's break down how it works and how you can implement it on your own store.
Understanding the Code
The code you provided consists of two main parts:
-
Setting the Minimum Order Amount: The $minimum
variable is set based on the customer's currency. If the currency is USD, the minimum is set to $14. Otherwise, it's set to 51.52 AED (United Arab Emirates Dirham).
-
Displaying the Error Message: If the customer's cart total is less than the minimum order amount, an error message is displayed. The message is different depending on whether the customer is on the cart page or the checkout page.
Here's a breakdown of the key parts of the code:
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
These lines of code hook the wc_minimum_order_amount()
function to two different WooCommerce actions: woocommerce_checkout_process
and woocommerce_before_cart
. This ensures that the minimum order amount check is performed both on the checkout page and the cart page.
function wc_minimum_order_amount() {
$currency = get_woocommerce_currency();
if ($currency == 'USD'){
$minimum = 14;
}else{
$minimum =51.52;
}
if ( WC()->cart->total < $minimum ) {
if( is_cart() ) {
wc_print_notice(
sprintf( 'You must have an order with a minimum of 52.5 AED Or 14 $ to place your order' ,
wc_price( $minimum ),
wc_price( WC()->cart->total )
), 'error'
);
} else {
wc_add_notice(
sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->total )
), 'error'
);
}
}
}
This is the main function that checks the cart total against the minimum order amount. It first retrieves the customer's currency using the get_woocommerce_currency()
function, and then sets the $minimum
variable accordingly.
Next, it checks if the cart total is less than the minimum order amount. If so, it displays an error message using the wc_print_notice()
or wc_add_notice()
functions. The error message includes the minimum order amount and the current cart total, formatted using the wc_price()
function.
The difference in the error message between the cart page and the checkout page is that the cart page displays a more concise message, while the checkout page provides more detailed information.
Implementing the Minimum Order Amount
To implement this functionality on your WooCommerce store, you can follow these steps:
-
Copy the code you provided and paste it into your theme's functions.php
file or a custom plugin.
-
Adjust the $minimum
values to suit your business requirements. For example, you might want to set the minimum order amount to $50 for USD and 200 AED for other currencies.
-
If you need to support additional currencies, simply add more elseif
statements to the if
block to set the appropriate minimum order amount for each currency.
-
Test the functionality thoroughly, both on the cart page and the checkout page, to ensure that the error messages are being displayed correctly and that the minimum order amount is being enforced as expected.
By implementing this code, you can ensure that your customers meet a minimum order threshold before completing their purchase, helping to improve the overall profitability of your WooCommerce store.
Flowpoint.ai can help you identify any technical errors or issues that may be affecting your website's conversion rates, and provide you with AI-generated recommendations to fix them. By integrating Flowpoint's powerful analytics and optimization tools, you can ensure that your WooCommerce store is running at its best and providing a seamless experience for your customers.