den*_*nko 5 php wordpress woocommerce
我需要更改 woocommerce 网站中的订单总重量。
例如:我的购物车中有 3 个产品:1 - 30g;2 - 35;3 - 35克;总计 = 30+35+35 = 100g,但我想将包装重量添加到总重量中(总重量的 30%)。
示例:((30+35+35) * 0.3) + (30+35+35) = 130g
我可以计算出来,但是如何将总重量从 100 克更改为 130 克。
为了获得总重量,我使用 get_cart_contents_weight(),但我不知道如何设置新值。
挂钩右侧过滤器操作
让我们看一下该函数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 );
}
Run Code Online (Sandbox Code Playgroud)
我们可以使用一个过滤器钩子:woocommerce_cart_contents_weight
所以我们可以向这个过滤器添加一个函数:
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;
}
Run Code Online (Sandbox Code Playgroud)
要分别为每个产品添加包装重量,您可以尝试以下操作:
add_filter('woocommerce_product_get_weight', 'add_package_to_product_get_weight');
function add_package_to_product_get_weight( $weight ) {
return $weight * 1.3;
}
Run Code Online (Sandbox Code Playgroud)
但不要同时使用两种解决方案。
| 归档时间: |
|
| 查看次数: |
2428 次 |
| 最近记录: |