Custom subject for New Order Email notification in Woocommmerce

扶醉桌前 提交于 2019-11-29 12:13:15

The Email settings for "New Order" the subject need to be (as in your question):
New Order - [{product_name}] ({order_number}) - {order_date}

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

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

Code goes in function.php file of your active child theme (or active theme).

Tested and works.


You will get something like this:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!