[英]Custom subject for New Order Email notification in WooCommerce
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}
在 WooCommerce 中,我想在“新订单”电子邮件主题行中设置购买的产品,如下所示:
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.我知道可能由于多个产品而无法使用
product_name
有没有一种方法我仍然可以通过过滤订购的产品或只允许多个产品来做到这一点,因为通过的多个订单并不多。
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…在下面的代码中,我将
{product_name}
替换为商品名称(以破折号分隔),因为一个订单可以包含多个商品……
This custom function hooked in woocommerce_email_subject_new_order
will do the trick:这个挂在
woocommerce_email_subject_new_order
自定义函数可以解决这个问题:
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).代码位于活动子主题(或活动主题)的 function.php 文件中。
Tested and works.测试和工作。
You will get something like this:你会得到这样的东西:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.