Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

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' 
            );

        }
    }

}

@nhui77777777

This comment has been minimized.

nhui77777777 commented Nov 19, 2017

yes it works.thanks

@jmdonaldson

This comment has been minimized.

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

This comment has been minimized.

jmdonaldson commented Jan 29, 2018

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

This comment has been minimized.

asaphtunes commented Mar 22, 2018

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