2016-11-11 30 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>'; 
}