在WooCommerce中设置根据购物车总重量计算的自定义运费价格 [英] Set a custom shipping rate cost calculated on cart total weight in WooCommerce

查看:331
本文介绍了在WooCommerce中设置根据购物车总重量计算的自定义运费价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce中,我安装了一个运输插件,但它计算购物车中每件商品的运费,而不是每笔订单的运费。

In Woocommerce I have installed a shipping plug in but it calculates shipping per item in cart instead of per order total.

我需要以这种方式计算运输成本:
-首公斤为$ 5
-其他每公斤$ 1。

What I need is to calculate the shipping cost this way: - First kg is $5 - And $1 by kilo for all others.

有可能做到吗?

使用该插件,如果购物车中有2个1公斤重的物品每个,它将运费计算为2 x $ 5 = $ 10

我需要的是: $ 5 (第一公斤) + $ 1 (额外公斤) = $ 6 (2公斤)

With the plugin if there is 2 items in cart of 1 kg each, it calculates the shipping as 2 x $5 = $10
Instead what I should need is: $5 (first kilo) + $1 (additional kilo) = $6 (for 2 kg).

我没有插件开发人员的回复,因此正在寻找解决方法。

I have no response from plugin developer, so looking for work around.

推荐答案


已更新:您不需要任何插件(因此可以将其删除)

UPDATED: You don't need any plugin (so you can remove it).

要获得以购物车总重量计算的运费,

To get a shipping rate calculated on total cart weight, with:


  • $ 5(第一公斤)的初始金额

  • 每增加一公斤的浮动费率$ 1

执行以下操作:

1)WooCommerce中的设置:

对于所有固定费率送货方式(在每个送货区域),将费用设置为 1 (按公斤计算)

1) Settings in WooCommerce:
For all your "Flat rate" shipping method (in each shipping zone), set a cost of 1 (amount by kilo).

因此,成本


(一次e禁用/保存并启用/保存以刷新woocommerce瞬态缓存)

2)自定义代码 (由于运输单位成本为每公斤$ 1,我们在计算中将购物车总重量增加4公斤以设置正确的成本)

add_filter( 'woocommerce_package_rates', 'custom_delivery_flat_rate_cost_calculation', 10, 2 );
function custom_delivery_flat_rate_cost_calculation( $rates, $package )
{
    // The total cart items  weight
    $cart_weight = WC()->cart->get_cart_contents_weight();

    foreach($rates as $rate_key => $rate_values){
        $method_id = $rate_values->method_id;
        $rate_id = $rate_values->id;
        if ( 'flat_rate' === $method_id ) {
            ##  SHIPPING COSTS AND TAXES CALCULATIONS ##
            $rates[$rate_id]->cost = $rates[$rate_id]->cost*($cart_weight+4);
            foreach ($rates[$rate_id]->taxes as $key => $tax){
                if( $rates[$rate_id]->taxes[$key] > 0 ){
                    $tax_cost = number_format( $rates[$rate_id]->taxes[$key]*($cart_weight+4));
                    $rates[$rate_id]->taxes[$key] = $tax_cost;
                }
            }
        }
    }
    return $rates;
}

代码包含在您活动的子主题的function.php文件中(

此代码已经过测试,可在woocommerce 2.6.x和3+版本上运行

This code is tested and works on woocommerce versions 2.6.x and 3+

这篇关于在WooCommerce中设置根据购物车总重量计算的自定义运费价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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