WooCommerce 中新订单电子邮件通知的自定义主题 [英] Custom subject for New Order Email notification in WooCommerce

查看:37
本文介绍了WooCommerce 中新订单电子邮件通知的自定义主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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 中新订单电子邮件通知的自定义主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆