問題描述
在 WooCommerce 中,我想將購買的產品設置為新訂單";電子郵件主題行,類似于:New Order - [{product_name}] ({order_number}) - {order_date}
我知道 product_name
可能由于多個產品而無法使用,我仍然可以通過過濾訂購的產品或僅允許多個產品來實現此目的,因為通過的多個訂單并不多.>
我對修改主題代碼很陌生.
新訂單"的電子郵件設置主題必須是(如您的問題):
新訂單 - [{product_name}] ({order_number}) - {order_date}
在下面的代碼中,我將 {product_name}
替換為商品名稱??(以破折號分隔),因為訂單可以有很多商品......
這個掛在 woocommerce_email_subject_new_order
中的自定義函數可以解決問題:
add_filter( 'woocommerce_email_subject_new_order', 'customizing_new_order_subject', 10, 2 );函數customizing_new_order_subject( $formated_subject, $order ){//獲取 WC_Email_New_Order 對象的實例$email = WC()->mailer->get_emails()['WC_Email_New_Order'];//從設置中獲取未格式化的主題$subject = $email->get_option('subject', $email->get_default_subject());//遍歷訂單行項目$product_names = array();foreach( $order->get_items() as $item )$product_names[] = ?$item->get_name();//在數組中設置產品名稱//在帶有分隔符的字符串中設置產品名稱(當多個項目時)$product_names = implode(' - ', $product_names );//替換{product_name}";按產品名稱$subject = str_replace( '{product_name}', ?$product_names, $subject );//格式化并返回自定義的格式化主題返回 $email->format_string( $subject);}
代碼位于活動子主題(或活動主題)的 function.php 文件中.
經過測試并有效.
你會得到這樣的東西:
In WooCommerce I would like to set the product purchased in the "new order" email subject line, something like this: New Order - [{product_name}] ({order_number}) - {order_date}
I understand that product_name
cant be used probably due to multiple products is there a way I can still do this by filtering product ordered or just allowing multiple products as not many multi orders go through.
I am very new to modifying theme code.
The Email settings for "New Order" the subject need to be (as in your question):
New Order - [{product_name}] ({order_number}) - {order_date}
In the code below I replace {product_name}
by the items product names (separated by a dash) as an order can have many items…
This custom function hooked in woocommerce_email_subject_new_order
will do the trick:
add_filter( 'woocommerce_email_subject_new_order', 'customizing_new_order_subject', 10, 2 );
function customizing_new_order_subject( $formated_subject, $order ){
// Get an instance of the WC_Email_New_Order object
$email = WC()->mailer->get_emails()['WC_Email_New_Order'];
// Get unformatted subject from settings
$subject = $email->get_option( 'subject', $email->get_default_subject() );
// Loop through order line items
$product_names = array();
foreach( $order->get_items() as $item )
$product_names[] = ?$item->get_name(); // Set product names in an array
// Set product names in a string with separators (when more than one item)
$product_names = implode( ' - ', $product_names );
// Replace "{product_name}" by the product name
$subject = str_replace( '{product_name}', ?$product_names, $subject );
// format and return the custom formatted subject
return $email->format_string( $subject );
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.
You will get something like this:
這篇關于WooCommerce 中新訂單電子郵件通知的自定義主題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!