問(wèn)題描述
在 WooCommerce 中,我想將購(gòu)買的產(chǎn)品設(shè)置為新訂單";電子郵件主題行,類似于:New Order - [{product_name}] ({order_number}) - {order_date}
我知道 product_name
可能由于多個(gè)產(chǎn)品而無(wú)法使用,我仍然可以通過(guò)過(guò)濾訂購(gòu)的產(chǎn)品或僅允許多個(gè)產(chǎn)品來(lái)實(shí)現(xiàn)此目的,因?yàn)橥ㄟ^(guò)的多個(gè)訂單并不多.>
我對(duì)修改主題代碼很陌生.
新訂單"的電子郵件設(shè)置主題必須是(如您的問(wèn)題):
新訂單 - [{product_name}] ({order_number}) - {order_date}
在下面的代碼中,我將 {product_name}
替換為商品名稱??(以破折號(hào)分隔),因?yàn)橛唵慰梢杂泻芏嗌唐?.....
這個(gè)掛在 woocommerce_email_subject_new_order
中的自定義函數(shù)可以解決問(wèn)題:
add_filter( 'woocommerce_email_subject_new_order', 'customizing_new_order_subject', 10, 2 );函數(shù)customizing_new_order_subject( $formated_subject, $order ){//獲取 WC_Email_New_Order 對(duì)象的實(shí)例$email = WC()->mailer->get_emails()['WC_Email_New_Order'];//從設(shè)置中獲取未格式化的主題$subject = $email->get_option('subject', $email->get_default_subject());//遍歷訂單行項(xiàng)目$product_names = array();foreach( $order->get_items() as $item )$product_names[] = ?$item->get_name();//在數(shù)組中設(shè)置產(chǎn)品名稱//在帶有分隔符的字符串中設(shè)置產(chǎn)品名稱(當(dāng)多個(gè)項(xiàng)目時(shí))$product_names = implode(' - ', $product_names );//替換{product_name}";按產(chǎn)品名稱$subject = str_replace( '{product_name}', ?$product_names, $subject );//格式化并返回自定義的格式化主題返回 $email->format_string( $subject);}
代碼位于活動(dòng)子主題(或活動(dòng)主題)的 function.php 文件中.
經(jīng)過(guò)測(cè)試并有效.
你會(huì)得到這樣的東西:
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:
這篇關(guān)于WooCommerce 中新訂單電子郵件通知的自定義主題的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!