2017-10-12 12 views

답변

1

주문 완료를 위해 woocommerce_order_status_completed 조치 훅을 사용할 수 있습니다. wp_insert_post을 사용하여 쿠폰의 게시물 유형이 shop_coupon 인 게시물을 만듭니다. 아래 코드를 확인하십시오.

function action_woocommerce_order_status_completed($order_id) { 

    $order = wc_get_order($order_id); 

    $order_items = $order->get_items(); 
    // Iterating through each item in the order 
    $item_quantity=0; 
    foreach ($order_items as $item_id => $item_data) { 

     $item_quantity=$order->get_item_meta($item_id, '_qty', true); 
     if($item_quantity>1){ 
      $product_ids[]=$item_data['product_id']; 
      $coupon_code = 'UNIQUECODE'.$order_id.$item_id; // Code 
      $amount = '10'; // Amount 
      $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product 

      $coupon = array(
       'post_title' => $coupon_code, 
       'post_content' => '', 
       'post_status' => 'publish', 
       'post_author' => 1, 
       'post_type'  => 'shop_coupon' 
      ); 

      $new_coupon_id = wp_insert_post($coupon); 

      // Add meta 
      update_post_meta($new_coupon_id, 'discount_type', $discount_type); 
      update_post_meta($new_coupon_id, 'coupon_amount', $amount); 
      update_post_meta($new_coupon_id, 'individual_use', 'no'); 
      update_post_meta($new_coupon_id, 'product_ids',$product_ids); 
      update_post_meta($new_coupon_id, 'exclude_product_ids', ''); 
      update_post_meta($new_coupon_id, 'usage_limit', ''); 
      update_post_meta($new_coupon_id, 'expiry_date', ''); 
      update_post_meta($new_coupon_id, 'apply_before_tax', 'yes'); 
      update_post_meta($new_coupon_id, 'free_shipping', 'no'); 
      unset($product_ids); 
     } 

    } 



}; 
// add the action 
add_action('woocommerce_order_status_completed', 'action_woocommerce_order_status_completed', 10, 1); 
+0

감사합니다. Ankur, 재생 용. 귀하의 솔루션을 시도하고 업데이트 해 드리겠습니다. 감사합니다. – Ketan

+0

안녕하세요, Ankur, 내 function.php 파일에서 위 코드를 추가 한 후 사이트가 작동하지 않으면 프런트 엔드와 백엔드 모두에서 빈 흰색 화면이 표시됩니다. 위의 코드를 검토하고 여기에서 업데이트 해주십시오. 감사합니다. – Ketan

+0

$ coupon_code = 'UNIQUECODE'$ order_id; 이 줄에 오류가 있습니다. UNIQUECODE와 $ order_id 사이에'.'을 추가하십시오. 수정 된 답변을 확인하십시오 –

0

woocommerce_order_status_completed 또는 woocommerce_order_status_processing 후크를 사용하여 주문을 완료하거나 배치 할 때 발생하는 기능을 작성하십시오. 함수 내에서 wp_insert_post을 사용하여 shop_coupon 유형의 게시물을 작성하십시오. wp_insert_post를 사용하여 쿠폰 제목, 금액 등을 지정할 수 있습니다.

+0

감사합니다. 귀하의 솔루션을 시도하고 업데이트 해 드리겠습니다. 감사합니다. – Ketan