dqu92800 2017-10-23 23:24
浏览 69
已采纳

多个PHP,如果声明只返回第一个条件,即使其他人遇到

Trying to add shipping insurance based on cart total in woocommerce with a multiple else if function. It seems only the first else if statement is working even when cart total is over $100. What am I missing?

    $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

  • 写回答

3条回答 默认 最新

  • dongyan5239 2017-10-23 23:29
    关注

    The first conditional block satisfies the criteria (if cart total is > $100, it's also > $50). Since the second block is an "else if", it will not be entered if the first condition is met. Try reversing the order of the conditions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题