WooCommerce

How to Set a Minimum Order Amount for Checkout

How to Set a Minimum Order Amount for Checkout

If you sell cheaper products in your store, and sometimes it happens when the shipping amount is more than the products cost in the order. Of course, you can send such orders, but often you have to set a minimum order amount to avoid such cases.

Fortunately for woocommerce it can be done very simply and you don’t need to install any additional plugins. Just add this code snippet to your child theme’s functions.php

First example: Set a Minimum Order Amount with Shipping Cost

//Set a minimum order amount for checkout
add_action( 'woocommerce_before_cart' , 'warp_minimum_order_amount' );
add_action( 'woocommerce_before_checkout_form' , 'warp_minimum_order_amount' );

function warp_minimum_order_amount()
{
    // Set this variable to specify a minimum order value
    $minimum = 1000;

    if ( WC()->cart->total < $minimum )
    {
        if( is_cart() || is_checkout() )
        {
            wc_print_notice(
                sprintf( esc_html__( 'Your current order total is %s — you must have an order with a minimum of %s to place your order', 'your text domain' ) ,
                    wc_price( WC()->cart->total ),
                    wc_price( $minimum )
                ), 'error'
            );
        }
    }
}

add_action( 'woocommerce_checkout_process', function()
{
    // Set this variable to specify a minimum order value
    $minimum = 1000;

    if ( WC()->cart->total < $minimum )
    {
        throw new Exception( sprintf( esc_html__( 'Your current order total is %s — you must have an order with a minimum of %s to place your order', 'your text domain' ) ,
            wc_price( WC()->cart->total ),
            wc_price( $minimum )
        ) );
    }
} );

Second example: Set a Minimum Order Amount without Shipping Cost

//Set a minimum order amount for checkout
add_action( 'woocommerce_before_cart' , 'warp_minimum_order_amount' );
add_action( 'woocommerce_before_checkout_form' , 'warp_minimum_order_amount' );

function warp_minimum_order_amount()
{
    // Set this variable to specify a minimum order value
    $minimum = 1000;

    if ( WC()->cart->subtotal < $minimum )
    {
        if( is_cart() || is_checkout() )
        {
            wc_print_notice(
                sprintf( esc_html__( 'Your current order total is %s — you must have an order with a minimum of %s to place your order', 'your text domain' ) ,
                    wc_price( WC()->cart->subtotal ),
                    wc_price( $minimum )
                ), 'error'
            );
        }
    }
}

add_action( 'woocommerce_checkout_process', function()
{
    // Set this variable to specify a minimum order value
    $minimum = 1000;

    if ( WC()->cart->subtotal < $minimum )
    {
        throw new Exception( sprintf( esc_html__( 'Your current order total is %s — you must have an order with a minimum of %s to place your order', 'your text domain' ) ,
            wc_price( WC()->cart->subtotal ),
            wc_price( $minimum )
        ) );
    }
} );

Comments (13)

  • Avatar

    omar

    |

    Hi
    Thanks for this! Really helpful.

    Do you know how to alter it to support currency conversion? I’m using woocommerce multi-currency atm. I’ve set the minimu at 2500 PKR, but if someone buys in USD, say $30 (which is more than 2500 PKR), it still thinks the minimum amount hasn’t been reached.

    Thanks in advance!

    Reply

    • Avatar

      Alex Podolyan

      |

      Hello Omar,

      You know this is a very good question 🙂

      You just need to replace this line

      $minimum = 1000;

      with this line

      $minimum = 0;
      $current_currency = get_woocommerce_currency();
      
      switch ($current_currency) {
          case 'PKR' :
              $minimum = 2500;
              break;
      
          case 'USD' :
              $minimum = 30;
              break;
      }

      Unfortunately, I do not know which plugin you use for multi-currency, but this example works fine with WPML multilingual if the multi-currency option is enabled.

      Best, Alex

      Reply

  • Avatar

    nas

    |

    Do you know how we can add an exception for a certain product?

    Reply

    • Avatar

      Alex Podolyan

      |

      I would do something like this.
      The example shows that if a product with a specific ID is added to the cart, the check will not work, but you can use this example for different situations.

      add_action( 'woocommerce_checkout_process', function()
      {
          // Set this variable to specify a minimum order value
          $minimum                = 1000;
          $check_subtotal         = true;
          $excluded_product_ids   = array(1,2,3);
      
          foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item )
          {
              if ( in_array( intval( $cart_item['product_id'] ), $excluded_product_ids, true ) )
              {
                  $check_subtotal = false;
                  break;
              }
          }
      
          if ( $check_subtotal && WC()->cart->subtotal < $minimum )
          {
              throw new Exception( sprintf( esc_html__( 'Your current order total is %s — you must have an order with a minimum of %s to place your order', 'your text domain' ) ,
                  wc_price( WC()->cart->subtotal ),
                  wc_price( $minimum )
              ) );
          }
      } );

      Reply

      • Avatar

        Marshall Sutherin

        |

        This is amazing. Is there a way it can be done without including tax? Right now if I add a 50 cent product it says the total is 55 cents.

        Reply

        • Avatar

          Alex Podolyan

          |

          In checking the minimum order amount, you can replace the line WC()->cart->subtotal to (WC()->cart->subtotal - WC()->cart->tax_total) and just take away the amount of taxes from the amount of the order

          Reply

      • Avatar

        Giovanni

        |

        Hi, great solution! Is there a way to show the error message (like wc_add_notice wrote in woocommerce official docs) if the excluded SKU is not present in the cart?

        Reply

        • Avatar

          Alex Podolyan

          |

          Hello,

          If I understand your question correctly, then yes, this can be done very simply and here’s an example of how I would do it.

          $show_error_messages = true;
          foreach ( WC()->cart->get_cart() as $cart_item )
          {
              $_product = $cart_item['data'];
          
              if( $_product->get_sku() === 'product SKU' )
              {
                  $show_error_messages = false;
              }
          }
          
          if( $show_error_messages )
          {
              throw new Exception( 'Error messages text' );
          }
          

          This code must be added to both functions.
          Alex

          Reply

  • Avatar

    Steve

    |

    Very nice! Thank you for posting this, worked like a charm… / running WP 5.2.2 / Woo 3.6.5

    Reply

  • Avatar

    Daniel

    |

    How can I make this minimum for just shipping not for pick up

    Reply

    • Avatar

      Alex Podolyan

      |

      Hi Daniel,

      I tried to get the shipping cost at this stage using these functions
      wc_get_chosen_shipping_method_ids()
      WC()->shipping->get_shipping_methods()

      I think it should be done on the checkout page and not in the cart and using some other Woo actions.
      Alex

      Reply

  • Avatar

    Matt

    |

    Can you do this for a specific product? I only have a couple of products on my site where I’d need this to be applied.

    Reply

    • Avatar

      Alex Podolyan

      |

      Hi there,

      I already described the solution and showed examples of how to do this for special products in the comments list for this post. Unfortunately, I don’t know all the details of what you want to do, so you can use the examples of my code yourself and do whatever you want.

      Best Alex

      Reply

Leave a comment

Subscribe to our Newsletter

Something BIG is coming, you'll be the first to know about it.