2013-12-18 99 views
2

我想添加費用根據數量。 對於例如:如果在購物= 5量,然後將要添加的費應該是4 $, 若,則待添加的費用量在購物= 7應爲8 $woocommerce根據數量增加到購物車的收費

我試圖此代碼獲取數量。

add_action('woocommerce_before_calculate_totals', 'woocommerce_custom_surcharge'); 

function woocommerce_custom_surcharge() { 
    global $woocommerce; 
$amount = $woocommerce->cart->get_cart_item_quantities(); 
if($amount==5) 
{ 
    $excost = 7; 
    } 
    else if($amount==7){ 
    $excost = 8; 
    } 
    $woocommerce->cart->add_fee('Charges delivery', $excost, $taxable = false, $tax_class = ''); 
} 

請幫助一致。

回答

3

請試試這個

function woocommerce_custom_surcharge(){ 
global $woocommerce; 
//Getting Cart Contents. 
$cart = $woocommerce->cart->get_cart(); 
//Calculating Quantity 
foreach($cart as $cart_val => $cid){ 
     $qty += $cid['quantity']; 
    } 
//Your Condition 
if($qty==5) 
{ 
    $excost = 7; 
    } 
    else if($qty==7){ 
    $excost = 8; 
    } 

    $woocommerce->cart->add_fee('Charges delivery', $excost, $taxable = false, $tax_class = ''); 


} 

From Here我發現,我們需要在總結cart.Hope的值,這將解決您的問題得到車的數量。

相關問題