woocommerce 根据数量向购物车添加费用 [英] woocommerce add fee to cart based on quantity
本文介绍了woocommerce 根据数量向购物车添加费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据数量添加费用.例如:如果购物车中的数量 = 5 ,则要添加的费用应为 4$,如果购物车中的数量 = 7,则要添加的费用应为 8$
I wan to add fee based on quantity. For eg: If quantity in cart = 5 , then the fee to be added should be 4$, If quantity in cart = 7, then the fee to be added should be 8$
我已尝试使用此代码来获取数量.
I have tried this code to get quantity.
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 = '');
}
请帮忙解决同样的问题.
Please help with the same.
推荐答案
请试试这个
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 I found that We need to get the quantity of the cart by summing up the values of the cart.Hope this will Solve your problem.
这篇关于woocommerce 根据数量向购物车添加费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文