2

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

这是我的代码:

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$.

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

4

2 回答 2

4

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

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

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. 在运输设置中,输入运输区域并禁用运输方式并“保存”。然后重新启用该运输方式并“保存”。你完成了。
于 2017-09-04T18:40:15.897 回答
0

下面的代码@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;
}
于 2019-06-14T13:18:10.573 回答