2016-06-30 4 views
0

내 고객 중 한 명은 새로운 사용자가 웹 사이트에 올 때 Rs. 500 할인을 받고 싶어합니다. 첫 번째 주문과 같은 할인은 쿠폰 코드를 입력 한 후 Rs. 250 할인을 받게됩니다. ..Woocommerce 첫 번째 및 두 번째 주문 할인 쿠폰

을 한 번에 사용되는 하나 개의 쿠폰을 그래서 당신은 어떻게 그렇게하는 경우 또는 어떤 플러그인을 사용할 수 제에 대한 그런 저를 제안 해주십시오 수와 두 번째 순서에 그는 또한

참고 Rs. xxxx의 쿠폰 할인을받을 2 차 할인.

+1

왜 게시물의 모든 단어의 첫 글자를 대문자로 표시합니까? 이것은 단지 당신의 질문을 읽기가 더 어렵게 만들고, 독자들을 자극하여 하향 회선으로 만들 수 있습니다. – halfer

+0

@halfer 나는 주제를 벗어나 명확하지 않고 너무 광범위하기 때문에 투표를 내려야 만한다. – LoicTheAztec

+0

@Loic, thanks. 나는 그것이 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 -by- 친절하게 : 1)이 코드는 작동하지 않습니다. 2) 대답 할 때 몇 가지 설명을해야합니다. 3) 코드를 선택했다면 링크와 참조를 포함하십시오. 그래서 OP와 커뮤니티에 유용하지 않기 때문에 이것을 피하십시오. – LoicTheAztec

+0

아니요, 블로그에서 가져 왔습니다. il check and and –

+1

이 코드를 직접 테스트 해 보셨습니까? – LoicTheAztec