钩在正确的筛选器操作
让我们对功能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
所以我们可以添加一个功能到这个过滤器:
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;
}
要包裹的重量分别添加到每一个产品,你可以试试这个:
add_filter('woocommerce_product_get_weight', 'add_package_to_product_get_weight');
function add_package_to_product_get_weight($weight) {
return $weight * 1.3;
}
但是不要使用这两种解决方案结合在一起。
它的工作原理,但当我计算航运时,我收到旧的重量值 – dendomenko
我已更新我的答案。尝试第二种解决方案。 –