过滤"woocommerce_cart_contents_weight"不适用于运输重量 [英] Filter "woocommerce_cart_contents_weight" not applied for shipping weight

查看:56
本文介绍了过滤"woocommerce_cart_contents_weight"不适用于运输重量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在购物车的总重量中考虑包裹重量,以便为您收取正确的运输费用.

I'm trying to take account of the package weight in the total weight of the cart so I can bill the right shipping cost.

我尝试在"woocommerce_cart_contents_weight"上添加过滤器,如

I tried adding a filter on 'woocommerce_cart_contents_weight' like explained there

所以我添加了以下代码段

So I added the following code snippet

function add_package_weight_to_cart_contents_weight( $weight ) {        
    $weight = $weight * 1.3; // add 30%     
    return $weight;     
}

add_filter('woocommerce_cart_contents_weight', 'add_package_weight_to_cart_contents_weight');

我还添加了一个片段来在购物车页面上打印购物车的重量,并且可以看到未应用重量过滤器:无论该片段是否处于活动状态,购物车的总重量都不会改变.

I've also added a snippet to print cart weight on cart page, and I can see that the weight filter isn't applied : The total weight of the cart doesn't change wether the snippet is active or not.

我还尝试将900g的物品准确地放入购物车,这样,随着箱子的重量,它超过了1kg并改变了价格,但运费仍然是1kg以下的价格.

I also tried putting exactly 900g articles in the cart so that, with the box weight, it goes over 1kg and change price, but the shipping price is still the one for under 1kg.

关于为什么不应用它以及如何解决它的任何想法?

Any idea on why it isn't applied and how I could fix it ?

推荐答案

您的代码有效,并且仅影响

Your code works and only affects WC_Cart method get_cart_contents_weight() usage.

现在采用运输方式,因为购物车物品可以分为包裹,所以重量是针对每个包裹计算的,并且不使用

Now for shipping methods, as cart items can be divided into packages, the weight is calculated for each shipping package and doesn't use WC_Cart method get_cart_contents_weight(). Instead it will make a calculation on the cart items for each package.

一种可能的方法应该是使用过滤器挂钩 woocommerce_package_rates 根据包裹重量计算来改变成本.但是,这似乎要复杂得多,因为您似乎正在使用第三方插件.

A possible way should be to use the filter hook woocommerce_package_rates to alter cost based on the package weight calculation. But this is going to be much more complicated as it seems that you are using 3rd party plugins.

这篇关于过滤"woocommerce_cart_contents_weight"不适用于运输重量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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