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

标签 php wordpress woocommerce orders email-notifications

在 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 文件。

经过测试并有效。


你会得到这样的东西:

enter image description here

关于php - WooCommerce 中新订单电子邮件通知的自定义主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48702040/

相关文章:

php - 如何使用 php 在新的 linux 分区上创建文件夹

php - 按共同好友数搜索 - 好友系统 Mysql PHP

javascript - Skype JavaScript 插件格式

wordpress - 我如何访问当前的 woocommerce 订单运输方式 ID?

jquery - 5 秒后关闭 jQuery 弹出模式

php - 如何相应地对这些数组进行分组?

php - 使用 session 生成的信息更新 mysql 表

php - Woocommerce `update_post_meta` 未更新数据库值

javascript - 为什么我的 wp_enqueue_scripts 没有触发

php - 在 Woocommerce 注册表中添加一个字段并在管理员中编辑用户