search

Home  >  Q&A  >  body text

Add product name in subject of WooCommerce new order email notification

<p>I want to change the subject line of the email I send to the store owner to put the product name in it. I saw this code to put the customer's name in front How can I adjust this code to include the product name</p> <pre class="brush:php;toolbar:false;">/* * Place it in the theme’s functions.php or custom plug-in * * Topic filter: *woocommerce_email_subject_new_order *woocommerce_email_subject_customer_processing_order *woocommerce_email_subject_customer_completed_order *woocommerce_email_subject_customer_invoice *woocommerce_email_subject_customer_note *woocommerce_email_subject_low_stock *woocommerce_email_subject_no_stock *woocommerce_email_subject_backorder *woocommerce_email_subject_customer_new_account *woocommerce_email_subject_customer_invoice_paid **/ add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2); function change_admin_email_subject( $subject, $order ) { global $woocommerce; $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); $subject = sprintf( '[%s] New customer order (# %s) from name %s %s', $blogname, $order->id, $order->billing_first_name, $order->billing_last_name ) ; return $subject; }</pre> <p>Maybe we just need to make changes here</p> <pre class="brush:php;toolbar:false;">$subject = sprintf( '[%s] New customer order (# %s) from name %s %s', $blogname, $item-> ;get_name, $order->billing_first_name, $order->billing_last_name ); return $subject; }</pre> <p><br /></p>
P粉990008428P粉990008428496 days ago755

reply all(1)I'll reply

  • P粉207483087

    P粉2074830872023-08-19 00:32:27

    Your actual code is outdated... To add the purchased product name (and quantity) to the subject of the new order email notification sent to the admin, use the following code:

    add_filter('woocommerce_email_subject_new_order', 'change_email_subject_new_order', 10, 2);
    function change_email_subject_new_order( $formatted_subject, $order ) {
        $products = array(); // 初始化
    
        // 循环遍历订单项目
        foreach( $order->get_items() as $item ){
            // 将格式化的产品名称和数量添加到数组中
            $products[] = sprintf( '%s &times; %d', $item->get_name(), $item->get_quantity() );
        }
    
        $count    = count($products); // 产品数量
        $products = implode(', ', $products); // 将数组转换为字符串
    
        return sprintf( 
            __('[%s] 新客户订单(#%s),%s,来自%s%s', 'woocommerce'), 
            wp_specialchars_decode(get_option('blogname'), ENT_QUOTES), $order->get_order_number(), 
            sprintf( _n('产品(%s)', '产品(%s)', $count, 'woocommerce'), $products, $products ),
            $order->get_billing_first_name(), $order->get_billing_last_name() 
        );
    }
    

    Place the code in your child theme’s functions.php file (or in a plugin). It has been tested and works fine.

    reply
    0
  • Cancelreply