2017-08-28 19 views
1

我注意到客戶暫停訂單電子郵件不可用,因此我試圖用一個操作替換髮送相應電子郵件的操作。以編程方式重新發送WooCommerce customer_on_hold_order電子郵件通知

這似乎工作,除了保持狀態。我沒有看到持有和處理案例之間的區別是什麼,除了$available_emails,class-wc-meta-box-order-actions.php之外,我已經刪除了所有其他的,他們仍然工作。

我做錯了什麼?這是否可以使這成爲可能?

我的代碼是:

function ulmh_resend1($actions) { 
    $actions['ulmh_resend'] = __('Resend Email', 'text_domain'); 
    return $actions; 
} 
function ulmh_resend2($order) { 
    $mailer = WC()->mailer(); 
    $mails = $mailer->get_emails(); 
    if ($order->has_status('on-hold')) { 
    $eml = 'customer_on_hold_order';  
    }elseif ($order->has_status('processing')) { 
    $eml = 'customer_processing_order'; 
    }elseif ($order->has_status('completed')) { 
    $eml = 'customer_completed_order'; 
    } else { 
    $eml = "nothing"; 
    } 
    if (! empty($mails)) { 
     foreach ($mails as $mail) { 
      if ($mail->id == eml) { 
       $mail->trigger($order->id); 
      } 
     } 
    } 
} 
function ulmh_resend3($order_emails) { 
    $remove = array('new_order', 'cancelled_order', 'customer_processing_order', 'customer_completed_order', 'customer_invoice'); 
    $order_emails = array_diff($order_emails, $remove); 
    return $order_emails; 
} 
add_action('woocommerce_order_actions', 'ulmh_resend1'); 
add_action('woocommerce_order_action_ulmh_resend', 'ulmh_resend2'); 
add_filter('woocommerce_resend_order_emails_available', 'ulmh_resend3'); 
+0

有什麼問題的時候,特別是?你的代碼中有什麼/不起作用? –

+0

如果訂單處於處理或已完成狀態,但代碼處於正常狀態,但處於暫掛狀態時未發送電子郵件,則代碼可以正常工作。沒有消息出現在調試日誌中,它看起來好像customer_on_hold_order不在$郵件中,但原始電子郵件發送正確 –

回答

1

我已經重新審視和壓縮你的代碼,因爲那裏有一些錯誤,如爲eml作爲變量名中if ($mail->id == eml){一個錯字錯誤...此外,從獲得訂單ID WC_Order您應該使用的對象$order->get_id()方法而不是$order->id

下面是這個新的功能代碼:

add_action('woocommerce_order_actions', 'ulmh_resend1'); 
function ulmh_resend1($actions) { 
    $actions['ulmh_resend'] = __('Resend Email', 'text_domain'); 
    return $actions; 
} 

add_action('woocommerce_order_action_ulmh_resend', 'ulmh_resend2'); 
function ulmh_resend2($order) { 
    $wc_emails = WC()->mailer()->get_emails(); 
    if(empty($wc_emails)) return; 

    if ($order->has_status('on-hold')) 
     $email_id = 'customer_on_hold_order'; 
    elseif ($order->has_status('processing')) 
     $email_id = 'customer_processing_order'; 
    elseif ($order->has_status('completed')) 
     $email_id = 'customer_completed_order'; 
    else 
     $email_id = "nothing"; 

    foreach ($wc_emails as $wc_mail) 
     if ($wc_mail->id == $email_id) 
      $wc_mail->trigger($order->get_id()); 
} 

add_filter('woocommerce_resend_order_emails_available', 'ulmh_resend3'); 
function ulmh_resend3($order_emails) { 
    $remove = array('new_order', 'cancelled_order', 'customer_processing_order', 'customer_completed_order', 'customer_invoice'); 
    $order_emails = array_diff($order_emails, $remove); 
    return $order_emails; 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

這個代碼在WooCommerce 3+測試,現在工作得很好的保持訂單狀態的電子郵件通知,重新發送

+0

感謝Loic - 我的兩個愚蠢的錯誤。需要更多的咖啡! –

相關問題