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

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

问题描述

在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.

对于修改主题代码来说还是很新的,但是任何帮助将不胜感激.

Very new to modifying theme code but any help would be greatly appreciated.

推荐答案

主题必须为新订单"的电子邮件设置(如您的问题):
New Order - [{product_name}] ({order_number}) - {order_date}

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 bellow 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.

您将获得以下内容:

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

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