在Woocommerce中禁用购物车税

时间:2018-12-04 22:25:29

标签: php wordpress woocommerce tax fee

在woocommerce中,我正在根据产品类别添加渐进式费用,并使用下面的代码(来自此answer thread

add_action( 'woocommerce_cart_calculate_fees', 'wc_custom_surcharge', 20, 1 );
function wc_custom_surcharge( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return; // Exit

    ## Your Settings (below) ##

    $categories      = array(19);
    $targeted_states = array('FL');
    $base_rate       = 1;

    $user_state = WC()->customer->get_shipping_state();
    $user_state = empty($user_state) ? WC()->customer->get_billing_state() : $user_state;
    $surcharge  = 0; // Initializing

    // If user is not from florida we exit
    if ( ! in_array( $user_state, $targeted_states ) )
        return; // Exit

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] )  ){
            // calculating fee based on the defined rate and on item quatinty
            $surcharge += $cart_item['quantity'] * $base_rate;
        }
    }

    // Applying the surcharge
    if ( $surcharge > 0 ) {
        $cart->add_fee( __("State Tire Fee", "woocommerce" ), $surcharge, FALSE, '' );
}
  

我的问题是我想将此费用设置为免税

我已经尝试了几个代码和其他东西,但只是不明白。

任何帮助或追踪都将不胜感激。

1 个答案:

答案 0 :(得分:0)

对于WC_Cart方法add_fee()附带的可用参数,您可以通过 2种方式处理此问题:

1)不征税:将费用设置为“不征税” (将第三个参数设置为false)

$cart->add_fee( __("State Tire Fee", "woocommerce" ), $surcharge, false );

2)使用“零税率”征税:使用“零税率” 税种将费用设置为“应税”

在这种情况下,您需要在Woocommerce Tx设置中将税率设置为零(如果尚未完成):

enter image description here

enter image description here

然后,您将可以在WC_Cart方法add_fee()中设置应税费用,并将第4个参数设置为“零费率” ,这是税收类别,方式:

$cart->add_fee( __("State Tire Fee", "woocommerce" ), $surcharge, true, 'zero-rate' );

其中一个应该起作用。

相关问题