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?
2
Answers
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:
It should work as expected.
Try this :
This solution should now work as expected