我想根据数量添加费用。 例如:如果购物车中的数量= 5,则要添加的费用应为4美元, 如果购物车中的数量= 7,则要添加的费用应为8 $
我已尝试使用此代码获取数量。
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 = '');
}
请帮助。
答案 0 :(得分: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我发现我们需要通过总结购物车的价值来获得购物车的数量。希望这能解决您的问题。