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

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

问题描述

在WooCommerce中,我想将购买的产品设置为新订单".电子邮件主题行,如下所示:New Order - [{product_name}] ({order_number}) - {order_date}

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}

我了解product_name可能由于多种产品而不能使用,我仍然可以通过过滤订购的产品或仅允许多种产品(因为没有多少个多重订单通过)来做到这一点.

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}

在下面的代码中,我将{product_name}替换为商品产品名称​​(用破折号分隔) ,因为订单中可以有很多商品…

In the code below I replace {product_name} by the items product names (separated by a dash) as an order can have many items…

钩在woocommerce_email_subject_new_order中的此自定义函数将完成此任务:

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 );
}

代码进入您的活动子主题(或活动主题)的function.php文件中.

经过测试,可以正常工作.

Tested and works.

您将获得以下内容:

这篇关于WooCommerce中新订单电子邮件通知的自定义主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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