多个PHP Else If语句仅返回第一个条件,即使满足其他条件
尝试在woocommerce中使用多个else if函数根据购物车总数添加运输保险。似乎只有第一个else if语句在购物车总额超过100美元时仍然有效。我错过了什么多个PHP Else If语句仅返回第一个条件,即使满足其他条件,php,wordpress,if-statement,woocommerce,Php,Wordpress,If Statement,Woocommerce,尝试在woocommerce中使用多个else if函数根据购物车总数添加运输保险。似乎只有第一个else if语句在购物车总额超过100美元时仍然有效。我错过了什么 $insurance_fee = 0; $chosen_methods = WC()->session->get( 'USPS_Simple' ); $chosen_shipping = $chosen_methods[0]; { if ( $woocommerce->cart->cart
$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, '' );
第一个条件块满足条件(如果购物车总计>100美元,则也>50美元)。由于第二个块是“else if”,因此如果满足第一个条件,则不会输入该块。尝试颠倒条件的顺序。第一个条件块满足条件(如果购物车总额大于100美元,则也大于50美元)。由于第二个块是“else if”,因此如果满足第一个条件,则不会输入该块。尝试颠倒条件的顺序。您必须添加购物车->购物车内容总量>=50&&$woocommerce->购物车->购物车内容总量<100){ $保险费=2.05; } 您必须添加购物车->购物车内容总量>=50&&$woocommerce->购物车->购物车内容总量<100){ $保险费=2.05; }
这是
if else if
的默认行为。当满足某个条件时,下一个条件将永远不会被计算为TRUE
如果要满足多个条件,可以使用
If
而不是If else If
这是If else If
的默认行为。当满足某个条件时,下一个条件将永远不会被计算为TRUE
如果要满足多个条件,可以使用
If
而不是If-else-If
这就是else-If的工作原理。。。如果它不符合上面的条件,而它又符合这个条件..这就是If的工作原理。。。如果它与上述条件不匹配,而与此条件匹配..DOH!当然,事情很简单。谢谢你帮了一个noobNo问题@ZachToth,如果你认为答案正确,请将其标记为正确。DOH!当然,事情很简单。感谢您帮助一个noobNo问题@ZachToth。如果您认为答案正确,请将其标记为正确。
if ( $woocommerce->cart->cart_contents_total >= 50 && $woocommerce->cart->cart_contents_total < 100 ) {
$insurance_fee = 2.05;
}