Skip to content
Advertisement

How to add product SKUs to WooCommerce new order email subject

Based on Woocommerce – Add username to new order email subject line. I’m trying to change WooCommerce email subject lines. Тhe email which I receive as site administrator in my mail when have a ordering.

I’m trying to create the following structure

[Online store]Order(#….)Name,Second name, Total-…,SKU1, SKU2, SKU3…

I use this code but I don’t know how to get the SKU for every each product in the order?

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] Order (# %s) от %s %s Total-%u EU. %s ', $blogname, $order->id, 
$order->billing_first_name, $order->billing_last_name, $order->order_total, $order->...);

return $subject;
}

Advertisement

Answer

You could use the following for this, the product SKUs are obtained via foreach loop from the order object.

Added comment with explanation to code

function filter_woocommerce_email_subject_new_order( $subject, $order ) {   
    // Blogname
    $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
    
    // Get order ID
    $order_id = $order->get_id();
    
    // Get billing first name
    $first_name = $order->get_billing_first_name();
    
    // Get billing last name
    $last_name = $order->get_billing_last_name();
    
    // Get order total
    $order_total = $order->get_total();
    
    // Empty array
    $product_skus = array();

    // Loop through order items
    foreach( $order->get_items() as $item ) {
        // Get an instance of corresponding the WC_Product object
        $product = $item->get_product();
        
        // Push to array
        $product_skus[] = $product->get_sku();
    }
    
    // Array to string
    $product_skus_str = implode( ", ", $product_skus );
    
    // Subject
    $subject = sprintf( '[%s] Order (# %s) From %s %s Total %s SKU %s', $blogname, $order_id, $first_name, $last_name, $order_total, $product_skus_str );

    return $subject;
}
add_filter( 'woocommerce_email_subject_new_order', 'filter_woocommerce_email_subject_new_order', 1, 2 );
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement