在"woocommerce_package_rates"中自定义税额钩 [英] Customize Tax amount in "woocommerce_package_rates" hook

查看:120
本文介绍了在"woocommerce_package_rates"中自定义税额钩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试用钩子修改我的所有运费,以应用折扣.

I recently tried to modify all my shipping rates with hook to apply discount.

这是我的代码:

add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates' );
function woocommerce_package_rates( $rates ) {
    $user_id = get_current_user_id();
    if ( ! wc_memberships_is_user_active_member( $user_id, 'silver' ) ) { return $rates; }
    $discount_amount = 30; // 30%

    foreach($rates as $key => $rate ) {
        $rates[$key]->cost = $rates[$key]->cost - ( $rates[$key]->cost * ( $discount_amount/100 ) );
    }

    return $rates;
}

但是,征税又是一步!我扣错了税.
例如,我有我的运费,费用为3$.有了折扣,现在是2,10$.

But one more step is the tax ! I got wrong tax.
For example I have my shipping rate who cost 3$. With the discount, it's now 2,10$.

我为2$和运输2.10$购买了一件商品. 我得到了1美元的税(3美元的运费.看起来他不接受更改),通常是0.82$.

I buy one item for 2$ and the shipping 2.10$. I got 1$ for the tax (as the 3$ shipping cost. look like he doesn't take the changes) and normally it's 0.82$.

我需要什么才能获得正确的税金计算?

What do I need to get the correct tax calculation?

推荐答案

更新:与运输方法的税费计算有关

Update: related to tax cost calculation for the shipping methods

您的代码上有一些小错误,您错过了税收计算折扣.我稍稍回顾了一下您的代码,您应该尝试执行以下操作:

There is some little errors on your code and you have missed the tax calculation discount. I have revisited your code a bit, you should try this:

add_filter( 'woocommerce_package_rates', 'conditional_shipping_discount', 10, 2 );
function conditional_shipping_discount( $rates, $packages ) {

    $user_id = get_current_user_id();
    if ( ! wc_memberships_is_user_active_member( $user_id, 'silver' ) ) return $rates;

    $percent = 30; // 30%
    $discount = 1 - ($percent / 100);

    foreach($rates as $rate_key => $rate_values ) {
        // Get original cost
        $original_cost = $rates[$rate_id]->cost;
        // Calculate the discounted rate cost
        $new_cost = $original_cost * $discount;
        // Set the discounted rate cost
        $rates[$rate_key]->cost = number_format(new_cost, 2);
        // calculate the conversion rate (for taxes)
        $conversion_rate = $new_cost / $original_cost;

        // Taxes rate cost (if enabled)
        $taxes = array();
        foreach ($rates[$rate_key]->taxes as $key => $tax){
            if( $tax > 0 ){ // set the new tax cost
                // set the new line tax cost in the taxes array
                $taxes[$key] = number_format( $tax * $conversion_rate, 2 );
            }
        }
        // Set the new taxes costs
        $rates[$rate_key]->taxes = $taxes
    }
    return $rates;
}

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

此代码已经过测试并且可以正常工作.

This code is tested and works.

您应该需要刷新运输缓存:
1)首先,此代码已经保存在您的function.php文件中.
2)在运送设置"中,输入运送区域并禁用运输方式和保存".然后重新启用该 Shipping Method (发货方式)并保存". 您已完成.

You should need to refresh the shipping caches:
1) First this code is already saved on your function.php file.
2) In Shipping settings, enter in a Shipping Zone and disable a Shipping Method and "save". Then re-enable that Shipping Method and "save". You are done.

这篇关于在"woocommerce_package_rates"中自定义税额钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆