I made this code to tell customers how far they are from getting free delivery, but it accounts for the total in the cart including VAT. I'd like to adjust according to the subtotal (without VAT). How do I adjust?
function free_shipping_notice(){
$min_amount = 2500;
$current = WC()->cart->subtotal;
if ( $current >= $min_amount ) {
wc_print_notice('You qualified for free delivery');
}else{
$added_text = '<p class="min-amount-text">Buy for ' . wc_price( $min_amount - $current ) . ' more to get free delivery</p>';
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
$notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ), $added_text );
wc_print_notice( $notice, 'notice' );
}
}
add_action( 'woocommerce_before_cart', 'free_shipping_notice', 10 );
Simply replace in your code WC()->cart->subtotal
with WC()->cart->get_subtotal()
to get cart subtotal excluding taxes (non discounted).
To get the discounted cart subtotal excluding taxes use WC()->cart->get_cart_contents_total()
.
Now you could also get subtotal from cart items as following too:
To get discounted cart subtotal excluding taxes you could use:
$subtotal = array_sum( wp_list_pluck( WC()->cart->get_cart(), 'line_subtotal' ) );
To get non discounted cart subtotal excluding taxes you could use:
$subtotal = array_sum( wp_list_pluck( WC()->cart->get_cart(), 'line_total' ) );