2013-08-30 179 views
0

我目前正在開發用於woocommerce插件,並有以下的代碼還增加了一個優惠碼如果總的車是在兩個值之間:woocommerce將車優惠券

add_action('woocommerce_before_cart', 'woobd_add_discount_if_enabled'); 

function woobd_add_discount_if_enabled() 
{ 
    global $woocommerce; 
    if ($woocommerce->cart->cart_contents_total >= 10 
     && $woocommerce->cart->cart_contents_total <= 100 
    ) { 
     $woocommerce->cart->add_discount('layer1_discount'); 
    } else {   
     $woocommerce->cart->remove_coupons('layer1_discount'); 
     $woocommerce->cart->calculate_totals(); 
    } 
} 

以上工作正常但是我想,如果其他語句添加像這裏面又增加了2個值:

add_action('woocommerce_before_cart', 'woobd_add_discount_if_enabled'); 

function woobd_add_discount_if_enabled() 
{ 
    global $woocommerce; 
    if ($woocommerce->cart->cart_contents_total >= 10 
     && $woocommerce->cart->cart_contents_total <= 100 
    ) { 
     $woocommerce->cart->add_discount('layer1_discount'); 
    } else {   
     $woocommerce->cart->remove_coupons('layer1_discount'); 
     $woocommerce->cart->calculate_totals(); 
    } 

    if ($woocommerce->cart->cart_contents_total >= 100 
     && $woocommerce->cart->cart_contents_total <= 200 
    ) { 
     $woocommerce->cart->add_discount('layer2_discount'); 
    } else {   
     $woocommerce->cart->remove_coupons('layer2_discount'); 
     $woocommerce->cart->calculate_totals(); 
    } 
} 

出於某種原因,第二if語句只得到檢測,完全無視第一個。有什麼明顯錯誤與上面?

回答

1

也許,如果你改變了兩個單獨的語句爲IF,ELSEIF,否則 - 它會工作。

add_action('woocommerce_before_cart', 'woobd_add_discount_if_enabled'); 

function woobd_add_discount_if_enabled() 
{ 
    global $woocommerce; 
    if ($woocommerce->cart->cart_contents_total >= 10 
     && $woocommerce->cart->cart_contents_total <= 100 
    ) { 
     $woocommerce->cart->add_discount('layer1_discount'); 
    } elseif ($woocommerce->cart->cart_contents_total >= 100 
     && $woocommerce->cart->cart_contents_total <= 200 
    ) {   
     $woocommerce->cart->add_discount('layer2_discount'); 
    } else {   
     $woocommerce->cart->remove_coupons('layer1_discount'); 
     $woocommerce->cart->remove_coupons('layer2_discount'); 
     $woocommerce->cart->calculate_totals(); 
    } 


}