0

I have tried several methods to add additional recipients to Woocommerce emails, but it only seems to work on test orders where the primary recipient is the admin.

These are the snippets I've tried. If the customer for the order is the admin, the email is sent both addresses. If the order contains a customer email address, it is only send to that email address and not the CC.

Here are the code snippets I've tried:

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

.

This one works, but fires with every single email notification:

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

If I add the email $object in, so it only fires for customer processing orders, it only cc's on the admin emails (cc only, not recipient), not customers (neither cc nor recipient).

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

I would appreciate any advice.

aleks1217
  • 87
  • 13

2 Answers2

2

The following code works in Woocommerce last version (v3.4.3) adding a custom email in "CC" for Customer processing email notification:

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

Code goes in function.php file of your active child theme (or active theme). Tested and work.

You can even add it to Bcc instead of Cc, like in this answer thread:
Adding custom emails to BCC for specific Woocommerce email notifications


The hook woocommerce_email_recipient_customer_processing_order doesn't seem to work in Woocommerce 3.4.x.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • This example isn't working properly, either. No CC goes out when using non-admin address, but in both cases the first email address isn't receiving an email. My logs say it was sent, but it isn't coming through. Can you test your code with addresses not related to your admin account, and tell me that it really works? Maybe it's my mail server, but I can't see how. For the record, I am using a private domain for the admin address, gmail for the customer address, and iCloud on the cc. – aleks1217 Jun 28 '18 at 17:50
  • On my test server it just works with different emails (from different domains)… I have already answered similar tested and accepted answers a lot of times. So there is something wrong in your config. – LoicTheAztec Jun 28 '18 at 18:09
  • That must be true, but I can't understand why. If I remove `if( 'customer_processing_order' !== $email_id ) return $header;` the cc works correctly, though if I change it to `'customer_completed_order'` the cc stops working again. I'll try turning all plugins/themes off and see if that makes a difference. – aleks1217 Jun 28 '18 at 18:41
  • Woocommerce Subscriptions is the culprit. Since `'customer_processing_renewal_order'` specifically overrides `'customer_processing_order'`, I thought it would be as simple as changing the `$email_id`, but no luck. I also tried working with the `'woocommerce_order_status_pending_to_processing_notification'` to request a secondary email is sent to an additional recipient with the `'emails/customer-processing-order.php'` template. In all cases, if Subscriptions is active it doesn't work, if Subscriptions is deactivated it does work. – aleks1217 Jun 29 '18 at 02:54
  • Looks like `'customer_processing_renewal_order'` was correct, except I had that email notification turned off on my testing site! (Could've saved two hours of puzzlement.) I'll post my answer below. Thank you @LoicTheAztec for being so responsive! – aleks1217 Jun 30 '18 at 01:00
  • As you didn't told in your question that it was related to subscriptions, my answer is just correct related to your initial question. Nobody can guess that. – LoicTheAztec Jun 30 '18 at 01:09
  • I totally agree. I didn't realize the problem was with the Subscriptions plugin until I was able to determine that by turning all plugins off and reactivating them. (I did mention that yesterday in my comments!) I'm sure you would have been able to solve it right away if you knew it had to do with Subscriptions. – aleks1217 Jun 30 '18 at 01:13
  • And it was quite strange how it worked for the admin, so I didn't think it was a plugin-related problem, because it was working in that case, oddly enough. – aleks1217 Jun 30 '18 at 01:17
0

The culprit was Woocommerce Subscriptions overriding the $email_id for customer_processing_order with customer_processing_renewal_order. After updating this text, both the headers and recipients were modifiable.

The headers hook, for Woocommerce Subscriptions:

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

And the recipient hook:

// If Woocommerce Subscriptions is active, hook needs the renewal email id
add_filter( 'woocommerce_email_recipient_customer_processing_renewal_order', 'my_email_recipient_filter_function', 10, 2);

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