如何调整 PHP 代码以获得不含增值税的 WooCommerce 购物车小计?

How to adjust PHP code to get WooCommerce cart subtotal without VAT?

我制作此代码是为了告诉客户他们距离免费送货还有多远,但它占了购物车中的总额,包括增值税。我想根据小计(不含增值税)进行调整。我该如何调整?

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

只需将您的代码 WC()->cart->subtotal 替换为 WC()->cart->get_subtotal() 即可获得不含税的购物车小计 (非折扣).

要获得不含税的折扣购物车小计,请使用 WC()->cart->get_cart_contents_total()

现在您还可以从购物车项目中获取小计,如下所示:

  • 要获得 折扣 购物车小计(不含税),您可以使用:

    $subtotal = array_sum( wp_list_pluck( WC()->cart->get_cart(), 'line_subtotal' ) );
    
  • 要获得 折扣购物车小计(不含税),您可以使用:

    $subtotal = array_sum( wp_list_pluck( WC()->cart->get_cart(), 'line_total' ) );