Woocommerce:添加第二个电子邮件地址不起作用,除非收件人是管理员

时间:2018-06-27 20:07:21

标签: php woocommerce email-notifications cc woocommerce-subscriptions

我尝试了几种方法将其他收件人添加到Woocommerce电子邮件中,但是它似乎仅适用于主要收件人为管理员的测试订单。

这些是我尝试过的片段。如果订单的客户是管理员,则将同时发送两个地址的电子邮件。如果订单包含客户电子邮件地址,则仅发送至该电子邮件地址,而不发送至抄送。

以下是我尝试过的代码段:

add_filter( 'woocommerce_email_recipient_customer_processing_order', 'my_email_recipient_filter_function', 10, 2);

function my_email_recipient_filter_function( $recipient ) {
$recipient = $recipient   . ', secondemail@example.com';
return $recipient;
}

add_filter( 'woocommerce_email_headers', 'woocommerce_email_cc_copy', 10, 2);

function woocommerce_email_cc_copy( $headers, $email ) {
if ( $email == 'customer_processing_order') {
    $headers .= 'CC: Your name <secondemail@example.com>' . "\r\n"; //just repeat this line again to insert another email address in BCC
}

return $headers;
}

此方法有效,但每一封电子邮件通知均会触发:

add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);

function mycustom_headers_filter_function( $headers, $object ) {
        $headers .= 'CC: My name <secondemail@example.com>' . "\r\n";

    return $headers;
}

如果我在其中添加电子邮件$object,则仅针对客户处理订单触发,仅在管理电子邮件中抄送(仅抄送,不包括收件人),而不是客户(抄送或收件人)。 >

add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);

function mycustom_headers_filter_function( $headers, $object ) {
 if ( $object == 'customer_processing_order') {
        $headers .= 'CC: My name <secondemail@example.com>' . "\r\n";
 }

    return $headers;
}

我将不胜感激。

2 个答案:

答案 0 :(得分:2)

以下代码在Woocommerce最新版本(v3.4.3)中适用:在“ CC”中添加自定义电子邮件,以便客户处理电子邮件通知:

add_filter( 'woocommerce_email_headers', 'custom_cc_email_headers', 20, 3 );
function custom_cc_email_headers( $header, $email_id, $order ) {

    // Only for "Customer Completed Order" email notification
    if( 'customer_processing_order' !== $email_id )
        return $header;

    // Prepare the the data
    $formatted_email = utf8_decode('Mister bean <misterbean@example.com>');

    // Add Cc to headers
    $header .= 'Cc: '.$formatted_email .'\r\n';

    return $header;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。

您甚至可以将其添加到密件抄送而不是抄送中,就像在此答案线程中一样:
Adding custom emails to BCC for specific Woocommerce email notifications


钩子woocommerce_email_recipient_customer_processing_order在Woocommerce 3.4.x中似乎不起作用。

答案 1 :(得分:0)

罪魁祸首是Woocommerce订阅以$email_id取代customer_processing_order的{​​{1}}。更新此文本后,标题和收件人都可以修改。

标题钩,用于Woocommerce订阅:

customer_processing_renewal_order

和收件人挂钩:

add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);

function mycustom_headers_filter_function( $headers, $object ) {

// If Woocommerce Subscriptions is active, this needs the renewal email id
 if ( $object == 'customer_processing_renewal_order') {
        $headers .= 'CC: My name <secondemail@example.com>' . "\r\n";
 }

    return $headers;
}