中禁用特定运输方式的税

中禁用特定运输方式的税

本文介绍了在 Woocommerce 中禁用特定运输方式的税的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a 3 shipping methods in my Woocommerce store and I want to disable the tax rate when customer choose the 3rd shipping method.

我该怎么做?

推荐答案

由于设置似乎无法从特定运输方式中去除税费,请尝试以下代码将设置零税金到您具体定义的运输方式:

As settings doesn't seem to be working to remove taxes from specific shipping methods, try the following code that will set zero taxes to your specific defined shipping methods:

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 中的相关运输方式和税费)...

This code goes on function.php file of your active child theme (or theme). Tested and works (if settings are made in a correct way, regarding related shipping methods and taxes in Woocommerce)

这篇关于在 Woocommerce 中禁用特定运输方式的税的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:04