将回复电子邮件地址更改为特定的 WooCommerce 电子邮件通知

Change reply-to email address to specific WooCommerce email notification

我正在尝试在向订单添加备注时向发送给客户的通知添加不同的回复电子邮件。 到目前为止我有:

add_filter( 'woocommerce_email_headers', 'add_replyto_emails_header', 10, 4 );
function add_replyto_emails_header( $headers, $email_id) {
    $replyto_email = 'store_owner@gmail.com';
    
$email_status = array( 'wc_email_customer_note',  'customer_note');
    if ( in_array( $email_id,  $email_status))  {
        $headers .= "Reply-To: " . $replyto_email . "\r\n";
    }

    error_log( print_r( $headers, true ) );
    return $headers;
}

邮件已发送,但邮件头未修改。我猜 email_status 数组中的状态不正确,我找不到任何其他对此电子邮件 ID 的引用。

如果您查看WC_Email get_headers() method source code, you will see that for all notifications send to the customer (so 'customer_note' email notification too), you get this related line code

$header .= 'Reply-to: ' . $this->get_from_name() . ' <' . $this->get_from_address() . ">\r\n";

现在 WC_Email get_from_name() method and get_from_address() method,每个过滤器钩子都有一个可用的过滤器钩子,您可以使用它来更改对姓名和电子邮件地址的回复,所以试试:

// Change reply to name
add_filter( 'woocommerce_email_from_name', 'change_reply_to_name', 10, 2 );
function change_reply_to_name( $from_name, $wc_email ){
    if( 'customer_note' === $wc_email->id ) {
        $from_name = ''; // Empty name
    }
    return $from_name;
}

// Change reply to adress
add_filter( 'woocommerce_email_from_address', 'change_reply_to_address', 10, 2 ); 
function change_reply_to_address( $from_email, $wc_email ){
    if( 'customer_note' === $wc_email->id ) {
        $from_email = 'store_owner@gmail.com';
    }
    return $from_email;
}

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