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

Php WooCommerce中新订单电子邮件通知的自定义主题,php,wordpress,woocommerce,orders,email-notifications,Php,Wordpress,Woocommerce,Orders,Email Notifications,在WooCommerce中,我想在“新订单”电子邮件主题行中设置购买的产品,类似这样:neworder-[{product\u name}]({order\u number})-{order\u date} 我知道,product\u name无法使用,可能是因为有多个产品。我是否仍然可以通过过滤订购的产品或只允许多个产品来实现这一点,因为没有多少多个订单通过 我对修改主题代码非常陌生。主题“新订单”的电子邮件设置需要如下(如您的问题所示): neworder-[{product\u name}

在WooCommerce中,我想在“新订单”电子邮件主题行中设置购买的产品,类似这样:
neworder-[{product\u name}]({order\u number})-{order\u date}

我知道,
product\u name
无法使用,可能是因为有多个产品。我是否仍然可以通过过滤订购的产品或只允许多个产品来实现这一点,因为没有多少多个订单通过

我对修改主题代码非常陌生。

主题“新订单”的电子邮件设置需要如下(如您的问题所示):

neworder-[{product\u name}]({Order\u number}){Order\u date}

在下面的代码中,我用项目产品名称(用破折号分隔)替换
{product_name}
,因为一个订单可以有许多项目

此自定义功能连接在
woocommerce\u email\u subject\u new\u 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文件

测试和工作


你会得到这样的结果:


hmm现在尝试代码,我得到一个错误。感谢您已经提供的帮助。解析错误:语法错误,在第23行的/home/*********/www/www/wp-content/themes/bb-theme-child/functions.php中出现意外的“$item”(T_变量),您应该始终提供您在问题中使用的woocommerce版本…那么第23行是什么呢…就我个人而言,我已经在WC 3.2.6上测试了这段代码,它工作得非常完美。WC 3.2.6和你一样,谢谢你的提示。还没有升级。第23行的代码是$产品名称[]=$项目->获取_name();//在列表中设置产品名称array@JamesPersson我已经重新测试了,它在我的测试服务器上运行得非常好…我添加了一个新订单电子邮件通知的屏幕截图,其中包含我重新发送的两个不同的项目…它的工作原理与只有一个项目时的工作原理相同。我的配置是WC 3.2.6 | WP 4.9.4 | PHP 5.6.32 | MySQL 5.6.38 | Theme Storefront…暂时不要更新WooCommerce,因为还有一些bug,很多主题和插件还没有准备好。WC版本3.3.1还不适合生产…@LoicTheAztec以上代码适用于woocommerce的最新版本