1

I would like the Email Subject line for the admin email to change based on the product category. I've looked at ALL similar stackoverflow 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;
}

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

0

The break is in the wrong place. When you put a break; in a loop (foreach), it quits the loop right at that point. You have your break before you set the $subject value so you're quitting the loop before the $subject is set. Set the subject, then break.

Also, your code snippet is missing the add_filter() call that triggers this filter function. This needs to be hooked to woocommerce_email_subject_new_order, the same as in the examples you linked to.

add_filter('woocommerce_email_subject_new_order', 'custom_admin_email_subject', 1, 2);    
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() ) ) { 
            $subject = sprintf( 'Category 1 Email Subject Line' );
            break;
        }
    } 
    return $subject;
}
  • Thanks for this comment. I tried your suggestion, but it still didn't output the desired Emil Subject line. I think there's something up with my has_term line. I'm still searching for the solution – david lee Nov 23 at 0:33
  • I tested this exact snippet (right down to using "Category 1 Name" as the category) and it worked fine. This may seem like too obvious a question, but do you have the add_filter() call in place that triggers this? Just for clarity's sake to make this answer more complete, I've edited the code above to include it rather than make that assumption – butlerblog Nov 23 at 20:37
  • Ok, it worked! I had the add_filter line in place, but I copied your code and pasted it into my functions.php file. Also, I was developing locally, so I decided to stop and restart my MAMP server. I don't know why it worked, but it did it. I don't know if was restarting the server, or if I had a syntax error, but I got it to work. Thank you! – david lee Dec 2 at 20:39
  • Glad to hear that it worked for you! – butlerblog Dec 2 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.