首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >根据WooCommerce中的cart小计将特定的应税运费设置为0

根据WooCommerce中的cart小计将特定的应税运费设置为0
EN

Stack Overflow用户
提问于 2020-11-19 20:09:33
回答 1查看 391关注 0票数 2

使用下面的代码,我可以使用特定的运费方法(这里是'easypack_parcel_machines')将成本设置为0,当cart小计达到特定金额时(在这里,150兹罗提)

代码语言:javascript
运行
复制
function override_inpost_cost( $rates, $package ) {
    // Make sure paczkomaty is available
    if ( isset( $rates['easypack_parcel_machines'] ) ) {
        // Current value of the shopping cart
        $cart_subtotal = WC()->cart->subtotal;
        // Check if the subtotal is greater than 150pln
        if ( $cart_subtotal >= 150 )    {
            // Set the cost to 0pln
            $rates['easypack_parcel_machines']->cost = 0;

        }
    }
    
    return $rates;
}

add_filter( 'woocommerce_package_rates', 'override_inpost_cost', 10, 2 );

但问题是,航运税的原始成本仍然存在,即使“easypack_parcel_machines”运输方法费率成本被设置为零其原始成本(因为它是应纳税的)。

如何更改代码,以便如果“easypack_parcel_machines”运输方法费率成本设置为0,则税收也将设置为零?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-19 20:58:41

注意: As cart可以通过一些插件或自定义代码分成多个配送包,正确的方法是获取当前配送包中包含的相关购物车项目的小计。

代码中缺少的是将税收设置为零,如下所示:

代码语言:javascript
运行
复制
add_filter( 'woocommerce_package_rates', 'override_inpost_shipping_method_cost', 10, 2 );
function override_inpost_shipping_method_cost( $rates, $package ) {
    $targeted_shipping_rate_id = 'easypack_parcel_machines'; // <== Define shipping method rate Id
    
    // Make sure that our shipping rate is available
    if ( isset( $rates[$targeted_shipping_rate_id] ) ) {
        $cart_subtotal_incl_tax = 0; // Initializing

        // Get cart items subtotal for the current shipping package
        foreach( $package['contents'] as $cart_item ) {
            $cart_subtotal_incl_tax += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
        }
        
        // Check if the subtotal is greater than 150pln
        if ( $cart_subtotal_incl_tax >= 150 )    {
            // Set the cost to 0pln
            $rates[$targeted_shipping_rate_id]->cost = 0;

            $taxes = array(); // Initializing

            // Loop through the shipping method rate taxes array
            foreach( $rates[$targeted_shipping_rate_id]->taxes as $key => $tax_cost ) {
                $taxes[$key] = 0; // Set each tax to Zero
            }

            if ( ! empty($taxes) ) {
                $rates[$targeted_shipping_rate_id]->taxes = $taxes; // Set back "zero" taxes array
            }
        }
    }

    return $rates;
}

代码位于活动子主题(或活动主题)的functions.php文件中。应该行得通。

注意:不要忘记清空您的购物车来刷新已缓存的数据。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64919234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档