WooCommerce 自定义运输成本最低要求小计与税收计算 [英] WooCommerce custom shipping costs for min required subtotal with tax calculations

查看:31
本文介绍了WooCommerce 自定义运输成本最低要求小计与税收计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WooCommerce 中设置了一些带有应税选项的运输方式.如果花费超过 X 金额,我使用此代码设置不同的运费:

I have set some Shipping methods with taxable option enabled in WooCommerce. I use this code to set up different shipping cost if spend over X amount:

// Extra discount on shipping for orders of values of above 150 or 100.
function custom_adjust_shipping_rate( $rates ) {
    $cart_subtotal = WC()->cart->subtotal;

    // Check if the subtotal is greater than value specified
    if ( $cart_subtotal >= 29.99 ) {

        // Loop through each shipping rate
        foreach ( $rates as $rate ) {

            // Store the previous cost in $cost
            $cost = $rate->cost;

            // Adjust the cost as needed
            // original shipping greater than 6 discount by 5 
            if ( $cost == 7.38 ) {
                // discount rate by 5
                $rate->cost = $cost - 2.54;
            }
            // Optional discount for other shipping rates 
            if ( $cost == 4.10 ) {
                $rate->cost = $cost - 5;
            }
        }
    }
    return $rates;
}
add_filter( 'woocommerce_package_rates', 'custom_adjust_shipping_rate', 10 );

但问题是税收计算.我的代码不处理税收.

But the problem is the tax calculations. My code doesn't handle taxes.

如何根据自定义运费调整税费?

How can I adjust taxes from custom shipping cost?

如何在成本计算中启用税收?

How to enable taxes in cost calculations?

感谢任何建议.

推荐答案

要在您的代码中启用运费税计算,请改用以下重新访问的代码:

To enable shipping taxes calculations in your code, use the following revisited code instead:

add_filter( 'woocommerce_package_rates', 'adjust_shipping_rates_cost', 10, 2 );
function adjust_shipping_rates_cost( $rates, $package ) {
    $min_subtotal  = 30; // Set min subtotal
    $cart_subtotal = 0; // Initializing
        
    // Loop through cart items to get items total for the current shipping package 
    foreach( $package['contents'] as $item ) {
        $cart_subtotal += $item['line_subtotal'] + $item['line_subtotal_tax'];
        // $cart_subtotal += $item['line_subtotal']; // Or without taxes
    }

    // Check if the subtotal is greater than specified value
    if ( $cart_subtotal < $min_subtotal ) {
        return $rates; // Exit
    }

    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ) {
        $has_taxes = false; // Initializing
        $taxes     = array(); // Initializing
        $new_cost = $initial_cost = $rate->cost; // grab initial cost

        if ( $initial_cost == 7.38 ) {
            $new_cost -= 2.54;
        } elseif ( $initial_cost == 4.10 ) {
            $new_cost -= 5;
        }
        $rates[$rate_key]->cost = $new_cost; // Set new rate cost

        // Loop through taxes array (change taxes rate cost if enabled)
        foreach ($rate->taxes as $key => $tax){
            if( $tax > 0 ){
                // Get the tax rate conversion
                $tax_rate    = $tax / $initial_cost;

                // Set the new tax cost in the array
                $taxes[$key] = $new_cost * $tax_rate;
                $has_taxes   = true; // Enabling tax changes
            }
        }
        // set array of shipping tax cost
        if( $has_taxes ) {
            $rates[$rate_key]->taxes = $taxes;
        }
    }
    return $rates;
}

代码位于活动子主题(或活动主题)的functions.php 文件中.它应该可以工作.

Code goes in functions.php file of the active child theme (or active theme). It should work.

不要忘记清空您的购物车以刷新运输缓存数据.

Don't forget to empty your cart to refresh shipping cached data.

这篇关于WooCommerce 自定义运输成本最低要求小计与税收计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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