禁用Woocommerce中特定送货方式的税

问题描述 投票:0回答:1

我的Woocommerce商店有3种送货方式,我想在客户选择第3种送货方式时禁用税率。

我怎样才能做到这一点?

wordpress woocommerce shipping-method
1个回答
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中的相关运输方法和税收)...

© www.soinside.com 2019 - 2024. All rights reserved.