skip to Main Content

I’m looking to change the subject line for the email that goes to shop owner to have product name in.
I saw this code that puts customer first name
how can I adjust this code to have product name

/*
 * goes in theme functions.php or a custom plugin
 *
 * Subject filters: 
 *   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;
}

Maybe we just have to change here

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

2

Answers


  1. Your actual code is really outdatedā€¦ To add the purchased product name(s) (and quantity) to the subject of the New Order email notification sent to the admin, use the following:

    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(); // Initializing
    
        // Loop through order items
        foreach( $order->get_items() as $item ){
            // Add formatted product name and quantity to the array
            $products[] = sprintf( '%s × %d', $item->get_name(), $item->get_quantity() );
        }
    
        $count    = count($products); // Products count
        $products = implode(', ', $products); // Convert the array to a string
    
        return sprintf( 
            __('New Customer Order (# %s), %s, from %s %s', 'woocommerce'),  
            $products,
            $order->get_billing_first_name(), 
            $order->get_billing_last_name() 
        );
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    Login or Signup to reply.
  2. Isn’t there one "%s" in excess in the line __(‘New Customer Order (# %s), %s, from %s %s’, ‘woocommerce’), ?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search