2014-09-22 74 views
0

我想對12(數量)應用折扣,並取消低於12(數量)的折扣。 我已經創建了一個優惠券代碼,可享受20%折扣('genew')。當點擊購物車頁面上的更新購物車按鈕(woo-commerce)時,我申請並取消了優惠券代碼。取消優惠券代碼功能僅在有人點擊兩次更新購物車按鈕時纔有效。首先點擊一下就不會刪除優惠券代碼。首次點擊購物車頁面上的更新購物車按鈕後,優惠碼不會被刪除woocommerce

這裏是我正在使用function.php功能

add_action('woocommerce_before_cart_table', 'discount_coupon'); 
function discount_coupon() { 
global $woocommerce; 
global $count_cart_quantity; 
if ($count_cart_quantity >= 12) { 
$coupon_code = 'genew'; 
if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) { 
    $woocommerce->show_messages(); 
} 

} 

if ($count_cart_quantity < 12 && $count_cart_quantity > 1) { 
$coupon_code = 'genew'; 
if (!$woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code))) { 
    $woocommerce->show_messages(); 
} 

} 

}

回答

0

您應該將這些條件語句合併成一個,全面發言:

add_action('woocommerce_before_cart_table', 'discount_coupon'); 
function discount_coupon() { 
    global $woocommerce; 
    global $count_cart_quantity; 

    $coupon_code = 'genew'; 

    if ($count_cart_quantity >= 12) { 
     if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) { 
      $woocommerce->show_messages(); 
     } 
    } 
    elseif ($count_cart_quantity < 12 && $count_cart_quantity > 1) { 
     if (!$woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code))) { 
      $woocommerce->show_messages(); 
     } 
    } 
} 
+0

請看到這個畫面拍攝。 http://imgur.com/Nid02c7 – craig 2014-09-22 09:04:06

+0

問題是,在12以下的數量減少後,它不會更新總量並給予20%的折扣。 如果我們在更改12個以下的數量後第一次點擊更新按鈕,那麼它不會更新總價格,但如果我們再次點擊更新購物車按鈕,那麼它會顯示正確的總數。 – craig 2014-09-22 09:18:24

相關問題