dongwei3151
2017-09-04 13:57
浏览 90
已采纳

在“woocommerce_package_rates”挂钩中自定义税额

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

Here's my code :

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

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

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?

图片转代码服务由CSDN问答提供 功能建议

我最近试图用挂钩修改我的所有运费以应用折扣。 < p>这是我的代码:

  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($ rate as $ key =&gt; $ rate){
 $ rates [$ key]  - &gt; cost = $ rates [$ key]  - &gt; cost  - ($ rates [  $ key]  - &gt;费用*($ discount_amount / 100)); 
} 
 
返回$ rates; 
} 
   
 
 

但还有一个 一步就是税! 我的税收错了。
例如我的运费是 3 $ 。 有了折扣,它现在是 2,10 $

我为 2 $ 和发货 2.10 $ 购买了一件商品。 我的税收为1美元(作为3 $运费。看起来他没有接受更改)通常它是 0.82 $

我需要什么来获得正确的税收计算?< / p>

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutongya8378 2017-09-04 18:40
    已采纳

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

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested and works.

    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.

    打赏 评论
  • duanjue2576 2019-06-14 13:18

    Below code @LoicTheAztec without mistakes:

    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;
    }
    
    打赏 评论

相关推荐 更多相似问题