Show Order Item Attribute Value in Email Subject for Woocommerce Products



Your code has been encountering errors since Woocommerce 3, particularly when dealing with orders that contain multiple items.

The following code will look for a particular product attribute (taxonomy) among order items. When it finds it, it will present a fresh custom subject with the product attribute term name value.

add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
function change_admin_email_subject( $subject, $order ) {
    // HERE define the product attribute taxonomy (start always with "pa_")
    $taxonomy = 'pa_color'; //
    // Loop through order items searching for the product attribute defined taxonomy
    foreach( $order->get_items() as $item ){
        // If product attribute is found
        if( $item->get_meta($taxonomy) ){
            // Custom new subject including the product attribute term name
            $subject = sprintf( '[%s] [%s] New customer order (# %s) from %s %s',
                get_term_by('slug', $item->get_meta($taxonomy), $taxonomy )->name, // Term name
                wp_specialchars_decode(get_option('blogname'), ENT_QUOTES),
            break; // Stop the loop
    return $subject;

The function.php file of your active child theme (or active theme) is where you should place your code. It has been tested and confirmed to work.

Frequently Asked Questions

Posted in Php