2016-12-10 7 views
2
私が支払いゲートウェイに基づいてオートコンプリート支払わ処理順序にWooCommerce from this answerにコードのこの小さな平和を使用してい

:これが動作しているいくつかの自動完了した注文に繰り返し電子メール通知を避け

/** 
* AUTO COMPLETE PAID ORDERS IN WOOCOMMERCE 
*/ 
add_action('woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1); 
function custom_woocommerce_auto_complete_paid_order($order_id) { 
    if (! $order_id) { 
     return; 
    } 

    $order = wc_get_order($order_id); 

    // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods. 
    if ((get_post_meta($order->id, '_payment_method', true) == 'bacs') || (get_post_meta($order->id, '_payment_method', true) == 'cod') || (get_post_meta($order->id, '_payment_method', true) == 'cheque')) { 
    return; 
    } 
    // "completed" updated status for paid Orders with all others payment methods 
    else { 
     $order->update_status('completed'); 
    } 
} 

ほとんど完璧

主にSMSの決済ゲートウェイをSMSで使用しています。このAPIは 'cod'支払い方法でブリッジされており、 'woocommerce_thankyou、out side frontend'の後に支払いを処理できます。その場合、保留ステータスオーダーは、その後処理ステータスに引き継がれます。これらの例のオートコンプリートの動作を自動化するために、私はthis answerからのコードのこの他の平和を使用し、それが動作します:

function auto_update_orders_status_from_processing_to_completed(){ 
    // Get all current "processing" customer orders 
    $processing_orders = wc_get_orders($args = array(
     'numberposts' => -1, 
     'post_status' => 'wc-processing', 
    )); 
    if(!empty($processing_orders)) 
     foreach($processing_orders as $order) 
      $order->update_status('completed'); 
} 
add_action('init', 'auto_update_orders_status_from_processing_to_completed'); 

問題:私は新しい完了した注文に関する繰り返し電子メール通知を取得しています。

このような繰り返しの電子メール通知を避けるにはどうすればよいですか?

おかげ

答えて

2

を繰り返し、電子メール通知のこの奇妙な事実を避けるために、ワードプレスupdate_post_meta()機能を使用して、完成に注文状況を変更する場合、各処理されたオーダーのカスタムメタキー/値を作成することが可能です。このカスタムメタデータキー/値が処理済み注文ごとにget_post_meta()の機能で存在する場合、条件で前にテストします。

だからあなたの2つのコードスニペットは、現在次のようになります。

1)自動補完は、支払方法に基づきWOOCOMMERCE(IN ORDERSを支払っ)

add_action('woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 20, 1); 
function custom_woocommerce_auto_complete_paid_order($order_id) { 
    if (! $order_id) 
     return; 

    // Getting the $order object from the ID 
    $order = wc_get_order($order_id); 

    // Getting the custom meta value regarding this autocomplete status process 
    $order_processed = get_post_meta($order->id, '_order_processed', true); 

    // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods. 
    if ((get_post_meta($order->id, '_payment_method', true) == 'bacs') || (get_post_meta($order->id, '_payment_method', true) == 'cod') || (get_post_meta($order->id, '_payment_method', true) == 'cheque')) { 
     return; 
    } 
    else 
    { 
     // We check if order has a status different than completed AND if the custom meta value hasen't been already set 
     if (!$order->has_status('completed') && $order_processed != 'yes'){ 
      // setting the custom meta data value to yes (order updated) 
      update_post_meta($order->id, '_order_processed', 'yes'); 
      $order->update_status('completed'); // Update order status to completed 
     } else { 
      return; 
     } 
    } 
} 

2)はSCANのALL "処理" 受注ステータス(自動完了)。

add_action('init', 'auto_update_orders_status_from_processing_to_completed'); 
function auto_update_orders_status_from_processing_to_completed(){ 

    // Get all current "processing" customer orders 
    $processing_orders = wc_get_orders(array(
     'numberposts' => -1, 
     'post_status' => 'wc-processing', 
    )); 

    if(!empty($processing_orders)){ 
     foreach($processing_orders as $order) { 
      // Checking if this custom field value is set in the order meta data 
      $order_processed = get_post_meta($order->id, '_order_processed', true); 
      if (!$order->has_status('completed') && $order_processed != 'yes') { 
       // Setting (updating) custom meta value in the order metadata to avoid repetitions 
       update_post_meta($order->id, '_order_processed', 'yes'); 
       $order->update_status('completed'); // Updating order status 
      } 
     } 
    } 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルになります。また、任意のプラグインのPHPファイルにもあります。

私はこのコードをテストしているようです。しかし、あなたの特定のSMSのブリッジされた支払方法のために私が同じ支払設定を持っていないので、私は完全に確信することができません。私はこれがうまくいくことを望みます。

+0

もう一度ありがとうLoïcそれは完璧に動作し、素敵な一日の男(y) –

関連する問題