0

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.

Sean Smith
  • 11
  • 1
  • 6
  • Which version of woocommerce… There is always a reason, so try to remember what you have maid… have you maid updates? have you maid changes or other customizations. have you added plugins? something has been making trouble… – LoicTheAztec Jan 31 '18 at 22:14
  • Thank you for the recommendation. I had this code inserted at the end of the functions.php file. I moved it to the top of the file and it is working fine. It seems as though maybe there is another part of the file that is having a problem and was keeping this code from working properly. I will now try and track down the source of that problem. – Sean Smith Feb 01 '18 at 00:32
  • So you should remove/delete this question then, as it's solved by something unexplained… – LoicTheAztec Feb 01 '18 at 00:34

0 Answers0