Search code examples
phpwoocommerceordersemail-notificationssubject

Change email subject for custom order statuses in Woocommerce 3


I have successfully changed email subject for Woocommerce processing order (using this thread):

add_filter( 'woocommerce_email_subject_customer_processing_order', 'email_subject_procs_order', 10, 2 );
function email_subject_procs_order( $formated_subject, $order ){
    return sprintf( esc_html__( 'Example of subject #%s', 'textdomain'), $order->get_id() );
} 

But I want send processing order email again with new subject after order status is changed, so I followed this tread to tweak subject etc.

add_action('woocommerce_order_status_order-accepted', 'backorder_status_custom_notification', 20, 2);
function backorder_status_custom_notification( $order_id, $order ) {
    // HERE below your settings
    $heading   = __('Your Awaiting delivery order','woocommerce');
    $subject = sprintf( esc_html__( 'New subject #%s', 'textdomain'), $order->get_id() );

    // Getting all WC_emails objects
    $mailer = WC()->mailer()->get_emails();

    // Customizing Heading and subject In the WC_email processing Order object
    $mailer['WC_Email_Customer_Processing_Order']->heading = $heading;
    $mailer['WC_Email_Customer_Processing_Order']->settings['heading'] = $heading;
    $mailer['WC_Email_Customer_Processing_Order']->subject = $subject;
    $mailer['WC_Email_Customer_Processing_Order']->settings['subject'] = $subject;

    // Sending the customized email
    $mailer['WC_Email_Customer_Processing_Order']->trigger( $order_id );
}

But only first email subject change is accepted. Is there way to get it work together?
Is if( $order->has_status( 'order-accepted' )) right to be used?


Solution

  • You need to use your custom status in a IF statement to avoid that problem, this way:

    add_filter( 'woocommerce_email_subject_customer_processing_order', 'email_subject_procs_order', 10, 2 );
    function email_subject_procs_order( $formated_subject, $order ){
        // We exit for 'order-accepted' custom order status
        if( $order->has_status('order-accepted') ) 
            return  $formated_subject; 
    
        return sprintf( esc_html__( 'Example of subject #%s', 'textdomain'), $order->get_id() );
    }
    

    Code goes in function.php file of your active child theme (or active theme). It should works.