Skip to content

Instantly share code, notes, and snippets.

@scottbuscemi
Created July 13, 2016 17:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottbuscemi/64211d3fd582ba2d55ef5c3977465c14 to your computer and use it in GitHub Desktop.
Save scottbuscemi/64211d3fd582ba2d55ef5c3977465c14 to your computer and use it in GitHub Desktop.
WooCommerce minimum order amount for subtotal only (don't include shipping+tax)
// Add to your functions.php file
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->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'
);
}
}
}
@davidysoards
Copy link

davidysoards commented Oct 5, 2017

This still includes the tax in the calculation of the subtotal and the display of the current order total.
For example. I had my min order set to $25, but two $12 items was enough to checkout, because of tax.
One $12 item would display a message stating, "your current order total is $12.72."
I was able to fix this by changing the "subtotal" to "subtotal_ex_tax"


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 = 25;

    if ( WC()->cart->subtotal_ex_tax < $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_ex_tax )
                ), '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_ex_tax )
                ), 'error' 
            );

        }
    }

}

@Eugene-pro-19
Copy link

yes it works.thanks

@jmdonaldson
Copy link

jmdonaldson commented Jan 29, 2018

Awesome stuff, thanks for the help
Just so you know, it causes issues with Braintree, won't allow me to enter my card details.

@jmdonaldson
Copy link

This method seems to work. Rather than calling the two hooks:

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );

Use this instead:
add_action( 'woocommerce_check_cart_items', 'wc_minimum_order_amount' );

It will stop you reaching the checkout unless purchase requirement is met

@asaphtunes
Copy link

Quick question:

How do i apply to this to international orders only. E.g. orders from America and canada I want to have it as minimum of 20 items need to be ordered. However for Australia have no minimum requirements?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment