在 Woocommerce 3 中更改自定义订单状态的电子邮件主题

Change email subject for custom order statuses in Woocommerce 3

我已成功更改 Woocommerce 处理订单的电子邮件主题 (使用

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() );
} 

但我想在订单状态更改后再次发送带有新主题的处理订单电子邮件,所以我按照 this tread 调整主题等

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 );
}

但只接受第一个电子邮件主题更改。有没有办法让它一起工作?
if( $order->has_status( 'order-accepted' ))用得对吗?

您需要在 IF 语句中使用您的自定义状态以避免该问题,这样:

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() );
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。