Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress woocommerce根据数量向购物车添加费用_Wordpress_Woocommerce - Fatal编程技术网

Wordpress woocommerce根据数量向购物车添加费用

Wordpress woocommerce根据数量向购物车添加费用,wordpress,woocommerce,Wordpress,Woocommerce,我想根据数量增加费用。 例如:如果购物车中的数量=5,则要添加的费用应为4美元, 如果购物车中的数量=7,则要添加的费用应为8$ 我试过这个代码来获取数量 add_action('woocommerce_before_calculate_totals', 'woocommerce_custom_surcharge'); function woocommerce_custom_surcharge() { global $woocommerce; $amount = $woocommer

我想根据数量增加费用。 例如:如果购物车中的数量=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 = '');
}
请帮我做同样的事情。

请试试这个

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 = '');


}
我发现我们需要通过汇总购物车的价值来获得购物车的数量。希望这能解决您的问题