【问题标题】:Customize Tax amount in "woocommerce_package_rates" hook在“woocommerce_package_rates”挂钩中自定义税额
【发布时间】:2018-02-12 18:49:40
【问题描述】:

我最近尝试使用挂钩修改所有运费以应用折扣。

这是我的代码:

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$

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

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

【问题讨论】:

    标签: php wordpress woocommerce shipping tax


    【解决方案1】:

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

    您的代码有一些小错误,您错过了计税折扣。我重新审视了你的代码,你应该试试这个:

    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 ($rate->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 文件或任何插件文件中。

    此代码已经过测试并且可以工作。

    您应该需要刷新运输缓存:

    1. 首先,此代码已保存在您的 function.php 文件中。
    2. 在配送设置中,输入配送区域并禁用配送方式并“保存”。然后重新启用运送方式并“保存”。 你已经完成了。

    【讨论】:

    • 哦,不错!非常感谢@LoicTheAztec !!完全符合我的需要
    • 我不明白,我用你的答案,这次税收计算不起作用。也许这段代码与某些东西有冲突?
    • @Efbi 我已经更新了关于税收计算的代码
    • 您好,出现了一些问题,1/ 费率部分隐藏了价格 2/ 当我输入税收部分时,我有空白页
    • 哎呀,这只是一个“;”错过了。谢谢它的作品!我保留以前的版本,因为它隐藏了价格并进行了调整
    【解决方案2】:

    下面的代码@LoicTheAztec没有错误:

    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_key]->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;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-07
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 2020-07-26
      • 1970-01-01
      • 2021-03-14
      • 2021-11-22
      相关资源
      最近更新 更多