問題描述
在我問這個問題之前,我知道有一個 (合法的) 猶豫,回答有關 Woo 產品的問題,因為他們有自己的支援,並且鼓勵使用者使用它。我是一個付費的 Woo 使用者,但是無法透過付出的支援來解決這個問題,而且我的問題是關於 WP 中的最重要的課程,所以我希望它會得到一個公平的聽證會。
我的問題:當一個完整的訂單電子郵件傳送給客戶時,我還需要逐字地收到這封電子郵件,並將其自動傳送到客戶,而不是像其他各種發票 PDF 建立的其他格式 WooCommerce 外掛。我可以很容易地透過更改/woocommerce/classes/emails/class-wc-email-customer-completed-order.php 中的以下行來完成此操作:
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
讀書:
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
$this->send( me@myemail.com, $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
但是,顯然,這樣的駭客並不會在升級中生存下去。我有一個覆蓋 WooCommerce 模板的子主題。有沒有任何等同的機制,我可以透過類似的封裝方式覆蓋一個類?或者您可以推薦一種替代方法 (除了將 SMTP 伺服器設定為將所有發出的電子郵件傳遞到第二個地址),以完成在客戶接收時收到此電子郵件的具體任務?
最佳解決方案
實際上可以使用一個過濾器,請參閱 abstract-wc-email.php 第 214 行:
return apply_filters( 'woocommerce_email_recipient_' . $this->id, $this->recipient, $this->object );
你可以將以下內容放在你的 functions.php 中:
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'your_email_recipient_filter_function', 10, 2);
function your_email_recipient_filter_function($recipient, $object) {
$recipient = $recipient . ', me@myemail.com';
return $recipient;
}
唯一的缺點是收件人會看到你的地址和他自己在 To:領域。
或者,建立在 Steve 的答案上,您可以使用 woocommerce_email_headers 過濾器。傳遞的 $物件允許您僅將其應用於已完成的訂單電子郵件:
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);
function mycustom_headers_filter_function( $headers, $object ) {
if ($object == 'customer_completed_order') {
$headers .= 'BCC: My name <my@email.com>' . "rn";
}
return $headers;
}
次佳解決方案
還有另一個過濾器可以讓您訪問 $ header 變數,這樣可以允許您將電子郵件傳送給 BCC,以便您獲得在 Woocommerce 上向客戶傳送的每封電子郵件的副本。這與上述程式碼一樣簡單,除非您的客戶端看不到您的電子郵件地址。
就像上面的解決方案,你將新增以下程式碼:
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);
function mycustom_headers_filter_function($headers, $object) {
$headers = array();
$headers[] = 'Bcc: your name <me@myemail.com>';
$headers[] = 'Content-Type: text/html';
return $headers;
}
此過濾器適用於所有 $標題,也適用於硬編碼型別為 text /html 。請注意,您不要在內容型別宣告中包含’/r/n’ – 這可能會導致 wp_mail() 中的錯誤 – 這是 Woocommerce 用於傳送訊息的方式。
我使用這個程式碼,所以我可以驗證 Woocommerce v2.0.14 。也應該在早期版本中工作,但不確定過濾器已包含多長時間。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。