Wooassist

Assistance for Your Woocommerce Store

  • How it Works
  • Pricing
  • Services
    • Site Maintenance
    • AI SEO
  • Blog
    • How-To Articles
    • Code Snippets
    • SEO For E-Commerce
    • Theme and Plugin Reviews
    • Wooassist News
    • WordPress/WooCommerce News
    • Interviews
  • About Us
  • Contact
You are here: Home / How-To Articles / How to Set a Minimum Order Amount for Specific Categories on WooCommerce

How to Set a Minimum Order Amount for Specific Categories on WooCommerce

August 25, 2023 By Rob Leave a Comment

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.

People that read this article also liked

How to Make WooCommerce Pages Full Width in Storefront Theme sticky-header-featureHow to Make a Sticky Header in Storefront Theme How to Center Logo in Storefront Theme_featureHow to Center Logo in Storefront Theme 11 Things You Can Do to Increase the Security of Your WooCommerce Store

Filed Under: Code Snippets, How-To Articles

Leave a Reply Cancel reply

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

Woocommerce Support

wooassist-team-in-Iloilo

Get 2 Hours of
FREE SUPPORT

We are so confident that you will love our services that we will give you your first 4 hours at a 50% discount

That's 4 hours for only $75

BUY NOW

Happy Wooassist Customers

Awesome! Fantastic! I just went to our site and I see that our site has been fixed!! We are getting orders again thanks to you all at WooAssist! Our site has not worked and looked this good for many months!! You all are awesome! Thank you so much for fixing our site! You have a customer for life! Thanks for making my day wonderful!

Kenneth Arnold

We have been quite pleased working with WooAssist as they help fill in the gaps of our development needs, all at a great price. They are always timely and communicate great, I highly recommend their services.

James Grasty

My husband and I am EXTREMELY pleased with the WooAssist Team. They provide excellent service, are very knowledgeable, and super easy to communicate with. The team ALWAYS has our company's best interests in mind. I love WooAssist! All of you make my job easier.

Jennifer Taft leetaft.com

Categories

  • Code Snippets
  • How-To Articles
  • Interviews
  • SEO For E-Commerce
  • Theme and Plugin Reviews
  • Wooassist News
  • WordPress/WooCommerce News

Recent Posts

  • WooCommerce Site Speed Optimization: How to Disable Lazy Loading for Images Above the Fold
  • Automating Blog Posting with AI-Generated Content for Your WooCommerce Store
  • The Future of AI in WooCommerce SEO: Emerging Trends and Predictions
  • How to Set a Minimum Order Amount for Specific Categories on WooCommerce
  • Will Google Penalize My Website for Having Content Written by an AI? Debunking the Myth
Let us support your online store so you can manage your business

Get started today

Get 2 Hours of FREE SUPPORT

We are so confident that you will love our services that we will give you your first 4 hours at a 50% discount

That's 4 hours for only $75

BUY NOW

Free eBook

Your subscription could not be saved. Please try again.
Your subscription has been successful.

YOURS FREE!

5 Things Every Online Store Can Fix On Their Website In The Next Week To Increase Sales

Quick Links

  • How it Works
  • Pricing
  • Blog
  • Contact
  • About Wooassist
  • My Account
  • Checkout
  • Privacy Policy
  • Cookie Policy
  • Terms and Conditions

Wooassist

Australia:
8 Storie St.
CLONTARF QLD 4019

Philippines:
San Miguel St.
Poblacion, Iligan City 9200

Connect

  • Facebook
  • LinkedIn
  • Twitter
  • YouTube

Copyright © 2023 · Wooassist

Yours FREE!

5 Things Every Online Store Can Fix On Their Website In The Next Week To Increase Sales