If you’re running an online store using WooCommerce, you may have specific requirements for minimum order amounts. For instance, you might want to set a minimum order value for a particular category of products. This ensures that customers must meet a certain threshold before they can proceed to checkout. In this blog post, we’ll walk you through a custom code snippet that will guide you on how to set a minimum order amount for specific categories in WooCommerce.
Set a Minimum Order Amount for Specific Categories on WooCommerce
To get started, you’ll need to access your WordPress theme files or use a child theme to add the following code. Open your theme’s functions.php file and paste the code snippet at the end:
/**
* Set a minimum order amount for checkout (Specific Category)
*/
add_action( 'woocommerce_checkout_process', 'woa_cat_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'woa_cat_minimum_order_amount' );
add_action( 'woocommerce_before_checkout_form' , 'woa_cat_minimum_order_amount' );
function woa_cat_minimum_order_amount() {
$minimum = 200; // Set this variable to specify a minimum order value for the target category
$term = 'target_category'; // Set this variable to specify the target category
// Utility function that counts the specific product category of cart items
$ctotal = 0; // Initialize cart total
$count = 0; // Counter variable
// Loop through cart items to check each category
foreach( WC()->cart->get_cart() as $cart_item ) {
if( has_term( $term, 'product_cat', $cart_item['product_id'] ) ) {
$count += $cart_item['quantity'];
$ctotal += $cart_item['line_subtotal'];
}
}
// Function that checks if the total order amount is less than the required order amount for the target category
if ( ($ctotal < $minimum) && ($count > 0) ) {
if( is_cart() ) { // If the user is viewing the cart page, display a warning and disable checkout
wc_print_notice(
sprintf( 'Your current order total for the Target Category is %s — you must have an order with a minimum of %s <span class="woocommerce-Price-amount amount">+ freight</span> to place your order.',
wc_price( $ctotal ),
wc_price( $minimum )
), 'error'
);
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 ); // Blocks the user from checking out
} else { // Display warning on all other pages
wc_add_notice(
sprintf( 'Your current order total for the Target Category is %s — you must have an order with a minimum of %s <span class="woocommerce-Price-amount amount">+ freight</span> to place your order.',
wc_price( $ctotal ),
wc_price( $minimum )
), 'error'
);
}
}
}
Breaking Down the Code
Let’s break down the code and how you can tweak it to work for your WooCommerce store.
The woa_cat_minimum_order_amount() function is hooked to three WooCommerce actions: woocommerce_checkout_process, woocommerce_before_cart, and woocommerce_before_checkout_form. These actions ensure that the minimum order amount check is performed during the checkout process.
Inside the function, you’ll find two variables that you need to customize:
$minimum: This variable represents the minimum order value you want to set for the target category. You can change the value according to your requirements.
$term: This variable holds the slug of the target category. Replace ‘target_category’ with the actual slug of the category you want to apply the minimum order amount to.
The code then initializes two variables, $ctotal and $count, to keep track of the total order amount and the number of products in the target category, respectively.
The code uses a loop to iterate through each item in the cart and checks if it belongs to the target category using the has_term() function. If it does, the product quantity and line subtotal are added to the respective variables.
After calculating the total order amount and the number of products in the target category, the code checks if the current order amount is less than the specified minimum and if there is at least one product from the target category in the cart.
If the conditions are met, the code displays a warning message to the user. If the user is on the cart page, the warning is shown, and the checkout button is disabled using remove_action(). If the user is on any other page, the warning is displayed without disabling the checkout button.
That’s it! By adding this custom code to your theme’s functions.php file, you can set a minimum order amount for a specific category in WooCommerce. Customers are notified about the minimum order requirement, ensuring that they meet the specified threshold before proceeding to checkout.
Remember to customize the $minimum and $term variables according to your needs. Additionally, if you want to restrict the minimum order requirement to specific user roles, you can uncomment the code block provided and modify it to suit your requirements.
Set a Minimum Order Amount Sitewide for WooCommerce
If you’re looking for a simpler implementation and just want a sitewide minimum order amount for your WooCommerce checkout, you can use the code below which is provided in the WooCommerce documentation.
/**
* Set a minimum order amount for checkout
*/
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// Set this variable to specify a minimum order value
$minimum = 50;
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 place your order ' ,
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 place your order' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
}
}
}
In the code snippet, you will notice a line that says $minimum = 50;. Here, you can set the desired minimum order value by replacing 50 with your preferred amount. For example, if you want to set the minimum order value to $100, change the line to $minimum = 100;.
Note: We recommend testing the code on a staging or development site before implementing it on a live site. This allows you to ensure that everything works as expected and doesn’t conflict with any other plugins or customizations you may have on your WooCommerce store.
If you need help implementing this functionality on your WooCommerce store, you can contact us. Our team can help.
Sam says
Hi;
I have four subcategories of products for example: fruits, pastries, candies, and canned goods which all fall under Food Items I can’t ship less than two items of food items it is not dependent on cost. how would I modify this code to look if the number of food items are two or more rather than the direct category?
Thank you
Rob says
Hi Sam,
This is possible but it will require a fair bit of change in the code. If you are interested, we can have our developers do this for you – customized especially for your needs.
You can also opt to get a plugin for this. The Min-Max Quantities plugin from WooCommerce should be able to do this.