尝试在woocommerce中添加基于购物车总额的运输保险,并使用多个其他功能。即使购物车总额超过100美元,它似乎也是第一个如果声明有效。我错过了什么?
$insurance_fee = 0;
$chosen_methods = WC()->session->get( 'USPS_Simple' );
$chosen_shipping = $chosen_methods[0]; {
if ( $woocommerce->cart->cart_contents_total >= 50 ) {
$insurance_fee = 2.05;
} else if ( $woocommerce->cart->cart_contents_total >= 100 ) {
$insurance_fee = 2.45;
} else if ( $woocommerce->cart->cart_contents_total >= 200 ) {
$insurance_fee = 4.60;
} else if ( $woocommerce->cart->cart_contents_total >= 300 ) {
$insurance_fee = 5.50;
} else if ( $woocommerce->cart->cart_contents_total >= 400 ) {
$insurance_fee = 6.40;
} else if ( $woocommerce->cart->cart_contents_total >= 500 ) {
$insurance_fee = 7.30;
}
}
//The fallback $insurance_fee value of 0 will be used if none of the conditions are met
$woocommerce->cart->add_fee( 'Shipping Insurance', $insurance_fee, true, '' );
Shipping insurance should return $2.45 since cart total is > $100
答案 0 :(得分:3)
第一个条件块满足条件(如果购物车总数> 100美元,它也是> 50美元)。由于第二个块是“else if”,如果满足第一个条件,则不会输入。尝试颠倒条件的顺序。
答案 1 :(得分:0)
您必须为语句添加< =。 像这样:
if ( $woocommerce->cart->cart_contents_total >= 50 && $woocommerce->cart->cart_contents_total < 100 ) {
$insurance_fee = 2.05;
}
答案 2 :(得分:0)
这是if else if
的默认行为。满足条件时,下一个条件永远不会被评估为TRUE。
如果您想要满足多个条件,可以使用if
代替if else if
。