2016-07-07 40 views
2

我正在編輯裝箱單,當訂單已付款和正在打包時生成裝箱單。如果包含特定類別的產品,則裝箱單將自動添加將打印在其上的優惠券。檢查WooCommerce優惠券是否已經存在

我的問題是,每次刷新或重新打開裝箱單時,都會使用相同的優惠券代碼生成新優惠券。所以我想實現一個規則,檢查coupon_code是否已經存在。

add_action('wpo_wcpdf_after_order_details', 'wpo_wcpdf_custom_text_categories', 10, 2); 
function wpo_wcpdf_custom_text_categories ($template_type, $order) { 
    // collect categories from order items 
    $order_cats = array(); 
    $items = $order->get_items(); 
    foreach ($items as $item_id => $item) { 
     $product = $order->get_product_from_item($item); 
     if ($product) { 
      $terms = get_the_terms($product->id , 'product_cat'); 
      foreach ($terms as $term) { 
       $order_cats[$term->term_id] = $term->name; 
      } 
     } 
    } 
    $target = array('Testtragen', 'Testtragetuch'); 
    // 
    // check for your category requirement 
    if(count(array_intersect($order_cats, $target)) > 0 && $template_type == 'packing-slip'){ 
     $coupon_code = $order->get_order_number(); 
     $discount_type = 'fixed_product'; // Type: fixed_cart, percent, fixed_product, percent_product  
     $order_date = get_post_meta($order->id, '_wcpdf_order_date', true); 
     $due_date = date_i18n(get_option('date_format'), strtotime($invoice_date . ' + 60 days')); 
     $email = $order->billing_email; 



     $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', ''); 
     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'); 
     */ 
    } 
} 

所以我想wp_insert_post($coupon)前添加if聲明,這一切只有代碼得到執行,如果與COUPON_CODE優惠券不存在。

我試過:term_exists($coupon_code, ‘coupons’)但這沒有奏效。

感謝您的幫助!

+0

你好,我很想說,它的工作原理。但不知何故,它什麼都不做。我完全理解代碼,它對我來說非常有意義。但是當我將它包含在我的函數中時,沒有任何反應,也沒有錯誤,也沒有改變程序。它完美的工作,並每次添加優惠券。 – clukas

+1

你的意思就像以前一樣...這不能避免,當刷新或重新打開裝箱單時,重新創建具有相同ID的優惠券?通常我的代碼不會做任何事情...它只是避免重新創建帶有相同ID的優惠券...如果在已創建此優惠券的訂單中存在'_wcpdf_coupon'meta_key,您可以檢查數據庫wp_postmeta表... – LoicTheAztec

+0

是,但我現在發現了這個問題。它沒有更新meta_key,因爲update_post_meta最後沒有布爾值。通過消除這一切工作正常。非常感謝!我可以按照它的方式接受你的回答嗎?或者你首先必須改變它? – clukas

回答

1

這裏是爲$order->id創建特殊meta_key的解決方案,以避免刷新或重新打開裝箱單時使用相同優惠券代碼生成新優惠券。

這是你改變的代碼(帶註釋解釋)

add_action('wpo_wcpdf_after_order_details', 'wpo_wcpdf_custom_text_categories', 10, 2); 
function wpo_wcpdf_custom_text_categories ($template_type, $order) { 
    // collect categories from order items 
    $order_cats = array(); 
    $items = $order->get_items(); 
    foreach ($items as $item_id => $item) { 
     $product = $order->get_product_from_item($item); 
     if ($product) { 
      $terms = get_the_terms($product->id , 'product_cat'); 
      foreach ($terms as $term) { 
       $order_cats[$term->term_id] = $term->name; 
      } 
     } 
    } 
    $target = array('Testtragen', 'Testtragetuch'); 
    // 
    // check for your category requirement 
    if(count(array_intersect($order_cats, $target)) > 0 && $template_type == 'packing-slip'){ 
     $coupon_code = $order->get_order_number(); 
     $discount_type = 'fixed_product'; // Type: fixed_cart, percent, fixed_product, percent_product  
     $order_date = get_post_meta($order->id, '_wcpdf_order_date', true); 
     $due_date = date_i18n(get_option('date_format'), strtotime($invoice_date . ' + 60 days')); 
     $email = $order->billing_email; 




     $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' 
     ); 


     // @@@ We create a meta_key '_wcpdf_coupon' for this order with value 'no' 
     if(empty(get_post_meta($order->id, '_wcpdf_coupon', true))) { 
      add_post_meta($order->id, '_wcpdf_coupon', 'no', true); 
     } 

     // @@@ if this meta_key for this order has a value = 'no' was update it to 'yes' 
     if(get_post_meta($order->id, '_wcpdf_coupon', true) == 'no') { 

      // Now we update it to 'yes'. This avoid the coupon be reused for this order. 
      update_post_meta($order->id, '_wcpdf_coupon', 'yes'); 

      $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', ''); 
      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'); 
     } 
    } 
} 

這應該解決這個問題...