2016-11-11 84 views
3

我需要更改woocommerce网站的订单总重量。更改woocomerce订单总重量

例如:我在购物车中有3件产品:1 - 30克; 2 - 35; 3 - 35g;总数= 30 + 35 + 35 = 100g,但我想增加包装重量到总重量(总重量的30%)。

实施例:((30 + 35 + 35)* 0.3)+(30 + 35 + 35)=130克

我可以计算它,但是如何从百克改变总重量至130g。

为了获得总重量我使用get_cart_contents_weight(),但我不知道如何设置新的值。

回答

2

钩在正确的筛选器操作

让我们对功能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; 
} 

但是不要使用这两种解决方案结合在一起。

+0

它的工作原理,但当我计算航运时,我收到旧的重量值 – dendomenko

+0

我已更新我的答案。尝试第二种解决方案。 –

0

它在我的工作。将总重量更新为新的重量值。

add_action('woocommerce_cart_collaterals', 'myprefix_cart_extra_info'); 
function myprefix_cart_extra_info() { 
    global $woocommerce; 
    echo '<div class="cart-extra-info">'; 
    echo '<p class="total-weight">' . __('Total Weight:', 'woocommerce'); 
    echo ($woocommerce->cart->cart_contents_weight*0.3)+$woocommerce->cart->cart_contents_weight; 
    echo '</p>'; 
    echo '</div>'; 
}