简体   繁体   English

WooCommerce 中新订单电子邮件通知的自定义主题

[英]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.

相关问题 自定义标题和主题 email 通知 WooCommerce 中的自定义订单状态 4 - Custom heading and subject email notification for custom order status in WooCommerce 4 在 WooCommerce 新订单 email 通知中添加运输 state - Add shipping state to subject in WooCommerce new order email notification 在不同的收件人上有条件的自定义新订单Woocommerce电子邮件通知 - Conditional custom New Order Woocommerce email notification on different recippients 在新订单Woocommerce电子邮件通知中添加自定义链接 - Add a custom link in new order Woocommerce email notification 如何将产品 SKU 添加到 WooCommerce 新订单电子邮件主题 - How to add product SKUs to WooCommerce new order email subject Woocommerce-将用户名添加到新订单电子邮件主题行 - Woocommerce - Add username to new order email subject line 自定义 email 主题占位符,用于 WooCommerce 订单相关预订 - Custom email subject placeholders for WooCommerce order related bookings 自定义管理员 Email 主题为 Woocommerce - Custom Admin Email Subject for Woocommerce 将产品自定义字段中的收件人添加到Woocommerce新订单电子邮件通知中 - Add recipients from product custom fields to Woocommerce new order email notification 将订单总重量(吨)转换为 WooCommerce 新订单 email 通知 - Convert the order total weight in tons to WooCommerce new order email notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM