【问题标题】:Set specific taxable shipping rate cost to 0 based on cart subtotal in WooCommerce根据 WooCommerce 中的购物车小计将特定的应税运费成本设置为 0
【发布时间】:2020-11-20 08:09:46
【问题描述】:

使用以下代码,当购物车小计达到特定金额(此处为 150波兰兹罗提)

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,税收也将设置为零?

【问题讨论】:

    标签: php wordpress woocommerce tax shipping-method


    【解决方案1】:

    注意:由于购物车可以通过一些插件或一些自定义代码拆分为多个运输包,正确的方法是获取当前运输包中包含的相关购物车项目的小计。 p>

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

    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 文件中。它应该可以工作。

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

    【讨论】:

    • 非常感谢!!!!!!!按预期工作!!!! :)
    猜你喜欢
    • 2022-06-17
    • 1970-01-01
    • 2019-05-06
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多