2016-06-30 40 views
0

我的一位客戶希望當新用戶訪問網站時,他們會得到Rs. 500的折扣。這是複雜的,折扣如第一次訂購時,他輸入優惠券代碼後得到Rs. 250折扣和二階他還獲得Rs. xxxx優惠券折扣Woocommerce一級和二級折扣優惠券

注:一次只能使用一張優惠券

所以,可以請你建議我該怎麼做,或者如果任何插件可用這樣的第一和。二次折扣。

+1

爲什麼你要在你的文章中每個單詞的首字母大寫?這隻會讓你的問題更難閱讀,而且可能會讓一些讀者感到憤怒,以至於他們會冷靜下來。 – halfer

+0

@halfer我只是改變這一點,但我倒投票,因爲脫離主題,不清楚和太寬... – LoicTheAztec

+0

@Loic,謝謝。我普遍贊成編輯即將關閉的開放式帖子,如果我認爲這對OP會產生教育影響。然而,在這種情況下,我確實懷疑過量的工作是否值得,但儘管如此,這項工作還是做得很好。 – halfer

回答

-1
/** 
* Frontend validate new customer only coupon code 
* hook: woocommerce_after_checkout_validation 
*/ 
add_action('woocommerce_after_checkout_validation','check_new_customer_coupon', 0); 

function check_new_customer_coupon(){ 
    global $woocommerce; 
    // you might change the firstlove to your coupon 
    $new_cust_coupon_code = 'firstlove'; 

    $has_apply_coupon = false; 

    foreach (WC()->cart->get_coupons() as $code => $coupon) { 
     if($code == $new_cust_coupon_code) { 
      $has_apply_coupon = true; 
     } 
    } 

    if($has_apply_coupon) { 

     if(is_user_logged_in()) { 
      $user_id = get_current_user_id(); 

      // retrieve all orders 
      $customer_orders = get_posts(array(
        'meta_key' => '_customer_user', 
        'meta_value' => $user_id, 
        'post_type' => 'shop_order', 
        'numberposts'=> -1 
      )); 

      if(count($customer_orders) > 0) { 
       $has_ordered = false; 

       $statuses = array('wc-failed', 'wc-cancelled', 'wc-refunded'); 

       // loop thru orders, if the order is not falled into failed, cancelled or refund then it consider valid 
       foreach($customer_orders as $tmp_order) { 

        $order = wc_get_order($tmp_order->ID); 
        if(!in_array($order->get_status(), $statuses)) { 
         $has_ordered = true; 
        } 
       } 

       // if this customer already ordered, we remove the coupon 
       if($has_ordered == true) { 
        WC()->cart->remove_coupon($new_cust_coupon_code); 
        wc_add_notice(sprintf("Coupon code: %s is only applicable for new customer." , $new_cust_coupon_code), 'error'); 
        return false; 
       } 
      } else { 
       // customer has no order, so valid to use this coupon 
       return true; 
      } 

     } else { 
      // new user is valid 
      return true; 
     } 
    } 

} 

可以幫助你。從一些blog得到。

+0

對不起,你已經複製了這個問題的代碼:http://stackoverflow.com/questions/38096816/how-do-i-programmatically-apply-a-coupon-in-woocommerce-for-first-order-made謹慎:1)此代碼不起作用。 2)當你回答時,你必須給出一些解釋。 3)如果您選擇了一些代碼,請包含鏈接和參考。所以請避免這種情況,因爲這對於OP和社區沒有用處。 – LoicTheAztec

+0

不,它來自一個博客。 il檢查和鏈接 –

+1

你自己測試這個代碼嗎? – LoicTheAztec