【问题标题】:Custom subject for New Order Email notification in WooCommerceWooCommerce 中新订单电子邮件通知的自定义主题
【发布时间】:2018-07-20 00:00:12
【问题描述】:

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

我知道product_name 可能由于多个产品而无法使用,有没有办法我仍然可以通过过滤订购的产品或只允许多个产品来做到这一点,因为没有多少多个订单通过。

我对修改主题代码很陌生。

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    主题必须是“新订单”的电子邮件设置(如您的问题):

    New Order - [{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 );
    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 文件中。

    经过测试并且有效。


    你会得到这样的东西:

    【讨论】:

    • 嗯,现在尝试代码,我得到一个错误。感谢您已经提供的帮助。解析错误:语法错误,第 23 行 /home/*********/www/www/wp-content/themes/bb-theme-child/functions.php 中的意外 '$item' (T_VARIABLE)
    • 您应该始终提供您在问题中使用的 woocommerce 版本……那么第 23 行是什么? ... 我个人已经在 WC 3.2.6 上测试过这段代码,它运行良好。
    • WC 3.2.6 喜欢你,谢谢你的提示。还没有升级。第 23 行的代码是....... $product_names[] = $item->get_name(); // 在数组中设置产品名称
    • @JamesPersson 我已经重新测试过,它在我的测试服务器上运行良好……我在一个订单上添加了一个新订单电子邮件通知的屏幕截图,其中包含我重新发送的 2 个不同的项目……它也能正常工作就像只有一件物品一样。我的配置是 WC 3.2.6 | WP 4.9.4 | PHP 5.6.32 | MySQL 5.6.38 |主题店面...不要更新 WooCommerce,因为仍然存在一些错误,并且许多主题和插件尚未准备好。 WC 3.3.1 版还不适合生产...
    • @LoicTheAztec 上面的代码是否适用于最新版本的 woocommerce
    猜你喜欢
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2018-04-23
    • 2016-03-03
    • 2018-01-07
    • 2020-09-09
    • 2020-11-22
    • 1970-01-01
    相关资源
    最近更新 更多