In WooCommerce, I am using the following code to set a mandatory minimum order amount for checkout:
/**
* Set a minimum order amount for checkout
*/
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 = 15;
$cart_total = WC()->cart->total; // Cart total incl. shipping
$shipping_total = WC()->cart->get_shipping_total(); // Cost of shipping
if ( ($cart_total - $shipping_total) < $minimum ) {
if( is_cart() ) {
wc_print_notice(
sprintf( 'Your current order total is %s — you must have an order with a minimum of %s excl shipping to place your order ' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
} else {
wc_add_notice(
sprintf( 'Your current order total is %s — you must have an order with a minimum of %s excl shipping to place your order' ,
wc_price( WC()->cart->total ),
wc_price( $minimum )
), 'error'
);
}
}
}
But I would like to use instead the cart subtotal excluding VAT and shipping. Actually I am getting the cart total including VAT and shipping.
How can I get the cart subtotal excluding VAT and shipping?
You need to get the cart subtotal excluding vat, using WC_Cart
get_subtotal()
method instead of using the "total" property.
Also, your code can be simplified a bit.
Try the following replacement:
/**
* Minimum order subtotal amount for checkout (excl shipping and taxes)
*/
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
$minimum = 15; // <=== Here specify the minimum order amount
$subtotal = WC()->cart->get_subtotal(); // Cart subtotal excl. shipping and taxes
if ( $subtotal < $minimum ) {
$message = sprintf(
esc_html__('Your current order subtotal is %s. To place your order, you must have a minimum amount of %s (excl. shipping and VAT).', 'woocommerce'),
wc_price( $subtotal ),
wc_price( $minimum )
);
if( is_cart() ) {
wc_print_notice( $message, 'error' );
} else {
wc_add_notice( $message, 'error' );
}
}
}
It should work as expected.