2

I would like the Email Subject line for the admin email to change based on the product category. I've looked at ALL similar stack overflow questions and NONE of them work for WooCommerece 3.8.0 (see this and this).

What I have is this

function custom_admin_email_subject( $subject, $order ) {
    global $woocommerce;
    foreach($order->get_items() as $item_id => $item ){
        if ( has_term( 'Category 1 Name', 'product_cat' , $item->get_product_id() ) ) { 
            break;
            $subject = sprintf( 'Category 1 Email Subject Line' );
        }
    } 
    return $subject;
}
add_filter('woocommerce_email_subject_new_order', 'custom_admin_email_subject', 1, 2);

My code simply returns the default email subject line for new orders (which is set in woocommerce/settings/email). I can't figure out why my function does not recognize category names.

Can anyone tell me what is wrong with my code?

I am placing this code in my child-theme/functions.php file I am running WooCommerce 3.8.0 and WordPress 5.3

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
David Lee
  • 571
  • 6
  • 20

1 Answers1

0

The issue is in your if condition. You should break the execution of foreach loop when product has term, only after setting the value in $subject variable. Rewrite your if condition as following. Have tested this and works :)

if( has_term( 'Category 1 Name', 'product_cat' , $item->get_product_id() ) ){ 
    $subject = sprintf( 'Hoodies Email Subject Line' );
    break;
}
melvin
  • 2,571
  • 1
  • 14
  • 37
  • Sadly, it didn't work. For some reason it is not overriding the default email subject line This is what I now have `function custom_admin_email_subject( $subject, $order ){ global $woocommerce; $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); foreach($order->get_items() as $item_id => $item ){ if( has_term( 'woo-cat-slug', 'product_cat' , $item->get_product_id() ) ){ $subject = sprintf( 'Custom Email Subject Line' ); break; } } return $subject; }` Any ideas why this is not working? – David Lee Nov 20 '19 at 19:57
  • The above code should work. Double check the slug name `woo-cat-slug` in your code. Confirm whether `woo-cat-slug` is the slug of the product category – melvin Nov 21 '19 at 03:25