Search

How To Set Minimum Cart Amount For User Roles In WooCommerce

    Nirav Mehta
Listen to this article

As a software developer who often deals with support requests of all shapes and sizes, a satisfied client is my ticket to a good night’s sleep. But a loyal and a recurring customer is what makes me get up the next morning and strive harder, learning and bettering myself in the process.

Customer loyalty is sign of confidence that what you’re doing is right, and the people who you’re doing it for, they like and trust your work. And expect you to keep doing it. Shep Hyken was absolutely on point when he said:

customer-loyalty-quote
Image Courtesy: Pinterest

WooCommerce is an exceptional e-commerce platform when it comes to tapping into features that build trust among your customers, and bring returns in the form of recurring customers and of course, more sales.

One way to do this is to price your products differently for customers belonging to varying membership levels or groups, the principle that led to the creation of one of our most successful products. This actually works on two levels:

[space]

  • Setting low prices for recurring customers ensures more conversions; a customer would be more than happy to buy a product at a much lower price than his peers.
  • Advertising that such a scheme exists on your WooStore acts as an incentive for fresh customers to turn into loyal shoppers and thus avail the low price scheme.

 

Set Minimum Order Amount for User Roles In WooCommerce

As an extension to this practice, you may also want to set different cart value limits for customers belonging to different user roles. This is particularly useful for when your store sells products in wholesale, and you only want to allow a user to go through with the purchase if the order amount exceeds a predefined value.

Minimum-Cart-Amount-Settings2

Conversely, you may let the users of select user roles to purchase products on your store, no matter the order amount.

Here’s how you can map different user roles to a minimum purchase cart amount on WooCommerce.

Step 1: Cart Page Validations:

  • Go to wp-content/plugins/WooCommerce/templates/cart directory and look up for cart-totals.php file
  • Copy this file and go to your theme/child theme, create a folder WooCommerce/cart and paste cart-total.php file here.
  • The structure will be something like /wp-content/themes/{Your-theme-name}/woocommerce/cart/cart-totals.php
  • After copy pasting the file, open the file in the editor and look for the code line:
<div class="wc-proceed-to-checkout">
      <?php do_action( 'woocommerce_proceed_to_checkout' ); ?>
 </div>

 

  • Replace the above code with the lines below.

 

   <div class="wc-proceed-to-checkout"> <?php
   //Get current user and check the user role.

   $current_screen_user = wp_get_current_user();

// In my scenario “wholesale_buyer” is the user role for which I want this validation. You can add your user roles.

    if( in_array( 'wholesale_buyer', $current_screen_user->roles ) ) {

      $minimum = 50; // Set the minimum amt.
      $cart_amt = WC()->cart->subtotal; // cart sub_total, this is actual total excluding discounts and shipping.

                if ( $cart_amt < $minimum ) {
          if( is_cart() ) {
        //Added notices for cart page.
              wc_print_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( $cart_amt )
                  ), 'error'
              );
          } else {
        //Added notice msg for checkout page.

              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( $cart_amt)
                  ), 'error'
              );
          }
      } else {
          do_action( 'woocommerce_proceed_to_checkout' );
      }
    } else {
      do_action( 'woocommerce_proceed_to_checkout' );
    }
    ?>
   </div>

 [space]

Step 2: Checkout Page Validations:

From the above code snippet, we hide the Proceed To Checkout button on cart page and add validations on cart page.

For the purpose of fool-proofing, we will also have to add the validations on checkout page, since a users with a little technical background could simply type in the URL of the page and bypass validations.

To do this, place the below code snippet in your themes ->  functions.php file.

 

<?php
add_action( 'woocommerce_checkout_process', 'wdm_wu_minimum_order_amount' );

function wdm_wu_minimum_order_amount() {

    $current_screen_user = wp_get_current_user();
    if( in_array( 'wholesale_buyer', $current_screen_user->roles ) ) {

      $minimum = 50;

    if ( WC()->cart->subtotal < $minimum ) {
       if( is_cart() ) {
           wc_print_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->subtotal )
               ), '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->subtotal )

               ), 'error'
           );
       } } } }

[space]

And that’s about it. Every user belonging to the specified user role will now be restricted to a minimum cart value while shopping on your website. Here’s how it would look in practice:

[space]

woocommerce-minimum-cart-amount

 

What’s amazing is that this needs not be limited to a single user role; play around with the code a bit and you can assign a minimum cart limit, and even create a settings page on the back end that would let you map user roles to minimum cart limits.


Found this article helpful? Let us know in the comment section below. Your feedback is what keeps us going. 🙂

 

 

Nirav Mehta

Nirav Mehta

9 Responses

  1. This is interesting. I am wanting to create a minimum purchase rule for various recurring purchases using Woosubscriptions. A monthly subscription and a bi-monthly subscription can be purchased in the same cart. I would like to require that each one of these meet the minimum. Is this possible. Thanks so much for sharing.

  2. Thanks for offering this, but when I follow the instructions, this does not validate on my Checkout page. I’m still able to bypass my cart and checkout.

    1. Stephen, I also thought that it did not work and that it really works.
      When the wholesale customer clicks on the “pay” button, it is then when the error message occurs.

  3. I am looking for EXACTLY this type of action. Allow me to put a different spin on this…

    I have a business that offers products to people within my LOCAL delivery area. Based on how far the customer lives, determines their minimum. I am considering managing this action via user roles (CustomerGroup1, CustomerGroup2, etc… where the minimum increases as the numeric value increases).

    What I would LOVE to see is this: That alert that tells the customer they have a minimum order to fill is a floating value that appears across the entire site as the customer navigates through different products. One the $$ amount minimum is met, it lets them know that they can proceed to checkout.

    I hope this post is still being monitored… Looking forward to someone replying that is a whole lot smarter than I am with coding.

  4. @izaman: just duplicate the code snippet for
    if( in_array( ‘wholesale_first_order’, $current_screen_user->roles ) ) {

    }
    and add it below with an elseif statement, ie
    elseif( in_array( ‘other_customer_role’, $current_screen_user->roles ) ) {

    }

  5. This is great, thank you. Just to help in addressing the concerns with it not working. The folder structure could be explained better. The instructions say to name the folder “woocommerce/cart”. As you may have noticed, you cannot name a folder with a “/”. Instead, name the folder “woocommerce” and then make a subfolder named “cart” and add your php file in there. Make sure you name the file cart-totals.php. Awesome piece of code!

Leave a Reply

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

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads