在 WooCommerce 中,我想在“新订单”电子邮件主题行中设置购买的产品,如下所示:New Order - [{product_name}] ({order_number}) - {order_date}
我知道不能使用 product_name
可能是因为有多个产品,我仍然可以通过过滤订购的产品或只允许多个产品来做到这一点,因为没有太多的多订单通过。
我对修改主题代码很陌生。
最佳答案
主题需要为“新订单”的电子邮件设置(如您的问题):
新订单 - [{product_name}] ({order_number}) - {order_date}
在下面的代码中,我将 {product_name}
替换为商品名称 (用破折号分隔),因为一个订单可以有很多商品……
Hook 在 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 );
}
代码进入您的事件子主题(或事件主题)的 function.php 文件。
经过测试并有效。
你会得到这样的东西:
关于php - WooCommerce 中新订单电子邮件通知的自定义主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48702040/