更改 woocommerce 订单总重量 [英] Change total woocommerce order weight
问题描述
我需要更改 woocommerce 网站中订单的总重量.
I need change total weight of order in woocommerce website.
例如:我的购物车中有 3 个产品:1 - 30g;2 - 35;3 - 35 克;总计 = 30+35+35 = 100g,但我想将包裹重量添加到总重量(占总重量的 30%).
For example: I have a 3 product in a cart: 1 - 30g; 2 - 35; 3 - 35g; total = 30+35+35 = 100g, but I want to add package weight to total weight (30% from total weight).
示例:((30+35+35) * 0.3) + (30+35+35) = 130g
Example: ((30+35+35) * 0.3) + (30+35+35) = 130g
我可以计算出来,但是如何将总重量从 100g 更改为 130g.
I can calculate it, but how change total weight from 100g to 130g.
为了获得总重量,我使用 get_cart_contents_weight(),但我不知道如何设置新值.
For getting total weight I use get_cart_contents_weight(), but I don't know how to set new value.
推荐答案
Hook in the right filter action
让我们看看函数get_cart_contents_weight()
:
public function get_cart_contents_weight() {
$weight = 0;
foreach ( $this->get_cart() as $cart_item_key => $values ) {
$weight += $values['data']->get_weight() * $values['quantity'];
}
return apply_filters( 'woocommerce_cart_contents_weight', $weight );
}
我们可以使用一个过滤器钩子:woocommerce_cart_contents_weight
There is a filter hook we can use: woocommerce_cart_contents_weight
所以我们可以给这个过滤器添加一个函数:
So we can add a function to this filter:
add_filter('woocommerce_cart_contents_weight', 'add_package_weight_to_cart_contents_weight');
function add_package_weight_to_cart_contents_weight( $weight ) {
$weight = $weight * 1.3; // add 30%
return $weight;
}
<小时>
要单独为每个产品添加包裹重量,您可以试试这个:
To add the package weight to every product separately, you can try this:
add_filter('woocommerce_product_get_weight', 'add_package_to_product_get_weight');
function add_package_to_product_get_weight( $weight ) {
return $weight * 1.3;
}
但不要同时使用这两种解决方案.
But do not use both solutions together.
这篇关于更改 woocommerce 订单总重量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!