Woocommerce: How to Set Purchase Amount Limit on Your Cart

Normally you won’t limit how much can your customer spend in your store. But somehow, for some reason, there are people want to limit the total amount of purchase in a store. Yes, I have encountered one. My client want to set a purchase limit in his Woocommerce site.

There already have plugins that can set maximum/minimum limit for the number of items that can be added to the cart, for each product, per order. But I didn’t found one that can limit the purchase amount, maybe there are, but I might have missed it.

Anyway, here is the solution that I come up with to set WooCommerce Purchase Amount Limit, with the help of `woocommerce_check_cart_items` action hook.

Woocommerce Set Cart Total Limits:

add_action( 'woocommerce_check_cart_items', 'customer_purchase_limit_check' );
function customer_purchase_limit_check(){
   if( is_cart() || is_checkout() ) {
       $maxamount = 10000;
       WC()->cart->calculate_totals();
       $cart_total = WC()->cart->total;
       if($cart_total > $maxamount ){
          wc_add_notice('You have exceeded the purchase limt ($100000) in a single order. Please try to edit your cart.', 'error');
       }
   }
}

This method can be apply to set the limit of total weight as well.

Woocommerce Set Cart Weight Limits:

add_action( 'woocommerce_check_cart_items', 'customer_purchase_weight_limit_check' );
function customer_purchase_weight_limit_check(){
     if( is_cart() || is_checkout() ) {
        $maxweight = 500;
        
        $cart_weight = WC()->cart->cart_contents_weight;
        if($cart_weight > $maxweight ){
            wc_add_notice('You cart is too heavy too ship.', 'error');

        }
    }
}

As you can see, this `woocommerce_check_cart_items`  can be use for validation. Which means you can add any kind of validation ‘Add To Cart’ action has been called.

6 thoughts on “Woocommerce: How to Set Purchase Amount Limit on Your Cart

  1. DEMİR AYDEMİR

    Hi, thanks for these simple solutions. If we want to specify the max amount for each cart per user basis like allowing them to add to their carts on their own according to their own predefined budgeting, what would you suggest?

    1. tck30

      You will need a field for your to enter his/her budget amount.
      Then in the above codes, change the `$maxamount` to the user budget. Of course, in order to get the user budget, the user must have to logged in first.

  2. Mohamed Talaat

    Hi,

    Thx for the code,

    but how can i set Maximum amount limit (on cart & check out) for 2 different currencies

    1. tck30

      Depends on how you implement the currencies.
      If it is from a plugin, then you need to check with that plugin to get the currency, and applied it to the snippet above.
      Or you can try with this function get_woocommerce_currency().

      And the snippet would be something like this:

      if( is_cart() || is_checkout() ) {
      $currency = get_woocommerce_currency();
      $maxamount = 10000000;//default max amount
      if($currency == 'USD'){
      $maxamount = 10000;
      }
      if($currency == 'EUR'){
      $maxamount = 5000;
      }
      /*
      if you have more then 2 currencies, you can add more if loop, or using switch case method.
      */
      WC()->cart->calculate_totals(); $cart_total = WC()->cart->total; if($cart_total > $maxamount ){
      wc_add_notice('You have exceeded the purchase limt ($100000) in a single order. Please try to edit your cart.', 'error');
      }
      }

      I never tested this snippet but you can get the idea.

  3. Daleon L

    Hmmm, I think this the code snippet that I am looking for but I think I am a bit stomp on it.

    Where do I add this code snippet, into a theme versioned site…?

    I would be alter this code snippet to make the minimum cart checkout limit to $150, else display a warning. What all I would needed changed?

    1. tck30

      You need to put it into your theme’s functions.php

      And the part you want to change is the $maxamount = 10000;
      change it to 150

      Note that you might want to change the warning message as well in this line:
      wc_add_notice('You have exceeded the purchase limt ($100000) in a single order. Please try to edit your cart.', 'error');

Leave a Reply to DEMİR AYDEMİR Cancel reply