为 WooCommerce 取消和失败的订单将管理员电子邮件设置为 BCC

Set admin email as BCC for WooCommerce cancelled and failed orders

我目前正在使用此代码发送失败和取消订单的通知。

function wc_cancelled_order_add_customer_email( $recipient, $order )
{ 
 return $recipient . ',' . $order->billing_email; 
} 
add_filter( 'woocommerce_email_recipient_cancelled_order', 'wc_cancelled_order_add_customer_email', 10, 2 ); 
add_filter( 'woocommerce_email_recipient_failed_order', 'wc_cancelled_order_add_customer_email', 10, 2 );

问题是我在 woocommerce 设置下的系统中设置的那两个管理员电子邮件也与客户电子邮件一起添加到电子邮件收件人中。 是否可以对此进行调整,让管理员电子邮件地址改为密件抄送,这样客户就看不到他们的电子邮件地址?

您需要对挂钩函数进行一些小改动,并添加一个额外的挂钩函数来处理作为密件抄送收件人的管理员电子邮件:

add_filter( 'woocommerce_email_recipient_cancelled_order', 'custom_cancelled_and_failed_order_email_recipients', 10, 2 ); 
add_filter( 'woocommerce_email_recipient_failed_order', 'custom_cancelled_and_failed_order_email_recipients', 10, 2 );
function custom_cancelled_and_failed_order_email_recipients( $recipient, $order ) { 
    // Check that the WC_Order object always exist
    if( is_a( $order, 'WC_Order' ) )
        $recipients = $order->get_billing_email(); 

    return $recipients;
} 


add_filter( 'woocommerce_email_headers', 'custom_cancelled_and_failed_order_email_headers', 20, 3 );
function custom_cancelled_and_failed_order_email_headers( $header, $email_id, $order ) {
    // Only for 'cancelled' and 'failed' order notifications
    if( in_array( $email_id, ['cancelled_order', 'failed_order'] ) ) { 
        // Get original admin recipient
        $recipient = WC()->mailer()->get_emails()['WC_Email_Cancelled_Order']->settings['recipient'];
        // Add Admin email As Bcc recipient
        $header .= 'Bcc: ' . $recipient . "\r\n";
    }
    return $header;
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。应该可以。