I am trying to have a custom email subject in the confirmation email sent when a customer buys a product from a specific category. Thanks to @LoicTheAztec I was able to get some code inserted in the functions.php and it seemed to work fine when first tested. But for some reason the code is no longer producing any results. Items purchased in this category have the standard woocommerce confirmation subject.
Here is the code I am using:
add_filter( 'woocommerce_email_subject_new_order', 'custom_subject_for_new_order', 10, 2 );
function custom_subject_for_new_order( $subject, $order ) {
$found = false;
// HERE define your product categories in the array (can be IDs Slugs or Names)
$categories = array('free-downloads'); // coma separated for multiples categories
// HERE define your custom subject for those defined product categories
$custom_subject = __("FREE DOWNLOAD ORDER CONFIRMATION", "woocommerce");
// Loop through order items
foreach( $order->get_items() as $item ){
if( has_term( $categories, 'product_cat', $item->get_product_id() ) ){
$found = true; // Category is found
break; // We stop the loop
}
}
// Return the custom subject when product category is found in order items
return $found ? $custom_subject : $subject;
}
Thank you for any help you can provide.