在Woocommerce中禁用特定运输方式的税款

时间:2018-09-03 08:09:50

标签: wordpress woocommerce shipping-method

我在Woocommerce商店中有3种运输方式,当客户选择第三种运输方式时,我想禁用税率。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

由于设置似乎无法从特定的运输方式中删除税款,请尝试以下代码将零税率设置为您定义的特定运输方式

add_filter('woocommerce_package_rates', 'null_specific_shipping_method_taxes', 12, 2);
function null_specific_shipping_method_taxes( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    // HERE define your targeted shipping methods IDs (in this array)
    $shipping_methods_ids = array( 'flat_rate:2', 'flat_rate:12', 'flat_rate:3', 'shipping_by_rules:5' );

    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ){
        $has_taxes = false;

        // Only for your defined Shipping method IDs
        if( in_array( $rate->id, $shipping_methods_ids ) ){
            $taxes = [];
            // Loop through the shipping taxes array (as they can be many)
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $rates[$rate_key]->taxes[$key] > 0 ){
                    // Set each tax cost to zero
                    $taxes[$key] = 0;
                    $has_taxes   = true;
                }
            }
            // Set the new taxes array
            if( $has_taxes )
                $rates[$rate_key]->taxes = $taxes;
        }
    }
    return $rates;
}

此代码位于您的活动子主题(或主题)的function.php文件中。经过测试并可以正常工作(如果以正确的方式进行设置,有关Woocommerce中的相关运输方式和税收)…