晚上好。我写了一些代码来修改我的购物车中的一些特定物品的总重量(由于快递的特定要求)。在你问之前...不,我不能修改单件重量,因为我必须计算一个特殊的 Package 重量。是的,这是一个很长的故事,
我的代码看起来像这样(简化版):
function set_my_weight($weight) {
global $woocommerce;
$cart_items = $woocommerce->cart->get_cart();
$addedWeight = 0;
echo "prev: ". $weight;
foreach($cart_items as $prod => $values):
if(conditions):
$addedWeight += 1.5;
endif;
endforeach;
$weight += $addedWeight;
echo "final: ". $weight;
return $weight;
}
add_filter('woocommerce_cart_contents_weight', 'set_my_weight',10,1);
当我响应调用echo WC()->cart->cart_contents_weight;
的值时,一切似乎都很好,但由于某种原因,运费没有改变。我正在使用“WooCommerce基于重量的运费”插件来管理我的运费。
为什么新的权重被插件忽略?
谢谢
3条答案
按热度按时间vnzz0bqm1#
由于WooCommerce基于重量的航运是第三方插件,有不同的方法来计算基于购物车重量的成本变化。
以下是更改推车重量的两种方法。
1)更改推车内容物总重量:
代码在您的活动子主题(或活动主题)的functions.php文件中。
2)更改购物车物品重量:
代码在您的活动子主题(或活动主题)的functions.php文件中。
现在,如果WooCommerce基于重量的运输插件从每个产品获得重量,您将无法更改购物车重量,因此运费。
k97glaaz2#
如果您使用的是WooCommerce Weight Based Shipping插件,
如果您在
WC()->cart->calculate_shipping()
(使用挂钩woocommerce_checkout_update_order_review
)之前更改购物车数据,@LoicTheAztec答案可能有效。资料来源:woocommerce/包含/class-wc-ajax. php
hvvq6cgz3#
过滤器woocommerce_cart_contents_weight不可用,因为get_cart_contents_weight不用于设置运费。解决方法是修改购物车中每个产品的重量。