4

How can I add BCC or CC to the woo commerce, new order email?

I think this is the code that is running the new order emails: http://codepad.org/kPTpSIM0 I cant seem to find what I need on Google. Thank you in advance.

I found this but I do not know where it goes:

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

function add_bcc_all_emails($headers, $object) {

$headers = array();
$headers[] = 'Bcc: Name <me@email.com>';
$headers[] = 'Content-Type: text/html';

return $headers;
}
Ajoy
  • 1,838
  • 3
  • 30
  • 57
LukeD1uk
  • 242
  • 2
  • 14

2 Answers2

7

Well it looks like you have already read this answer as that is the recommended code, though I believe it will add the BCC on all emails and not just new orders.

Instead I would suggest the following:

add_filter( 'woocommerce_email_headers', 'add_bcc_all_emails', 10, 3);

function add_bcc_all_emails( $headers, $email_id, $order ) {

    if( 'new_order' == $email_id ){
        $headers .= "Bcc: Name <me@email.com>" . "\r\n";
    }

    return $headers;
}

As for "where the code goes", it should be made into a plugin. Depending on how easily you would like to be able to disable this code in the future you could write it as a regular plugin (disable from Plugins overview) or as a site-specific snippets plugin (permanent until you delete file via FTP).

Community
  • 1
  • 1
helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • Mmm, I really want this.... However it will not work. I put the snippet im my functions.php but no bcc is received. I added the right name and email address to the code. What am I doing wrong? Please advice. – A3O Jan 07 '16 at 14:56
  • Try again with the updated code. I wasn't using `$email_id` consistently and so `$id` was undefined. – helgatheviking Jan 07 '16 at 15:54
  • Thank you for your comment. I've tried it but no Avail. Sorry. There is still no bcc received. – A3O Jan 07 '16 at 16:45
  • 1
    I tested it and confirm that it works for the new order email. If you always want a BCC for every email then you need to remove the conditional logic. – helgatheviking Jan 07 '16 at 17:21
  • Thank you. as a matter of fact I want it on the customer_completed_order email. But in the new_order email as in the above mentioned one it is not working. I have to investigate further. – A3O Jan 07 '16 at 17:32
  • Hi helgatheviking. I did a lot of further investigation sadly with no result. I went up to deactivate all my plugins, except for woocommerce with the same result. I'm developing locally so I mistrusted that situation but even on an online spot it simply did not work. Do you have any other suggestion? Please? PS, sorry to disturb you again with this. – A3O Jan 08 '16 at 16:32
  • I have no further suggestions. Please see [working demo](https://wordpress-helgatheviking.c9.io/product/woo-single-1/). You can buy any fake product and checkout with all fake data (stripe testing credit card of 4242 4242 4242 4242). The new order email that goes to the admin will be BCC'd to `bcc@mailinator.com` a disposable email account that can be checked [here](https://mailinator.com/inbox.jsp?to=bcc). It takes a minute to show up at mailinator, but it does show up. Just a heads up, that this demo will not remain live for very long. – helgatheviking Jan 08 '16 at 17:01
  • If you are developing locally, are you sure that you have send mail working? I do not and have to use [Papercut](https://papercut.codeplex.com/) to test local emails. – helgatheviking Jan 08 '16 at 17:03
  • Thank you for your help and patience. I did create an order on your sandbox. I received the new order email, was able to checkout through stripe testing, got the order complete email. I also say the email coming up on mailinator (I deleted it also). So it works on your site. I never mistrusted you on that. I have outgoing mail working. The new_order email is send, the customer_order_complete email is send, just not the bcc suggested in the function. Like I said: it lives in my (child) themes function.php. That is the right place to put it isn't it? Thanks for your help anyway. – A3O Jan 08 '16 at 17:38
  • Yes, that's where it belongs. I am also using a child theme and the code is in it's `functions.php`. Unfortunately, I don't know what would be different between my demo and you local server. For basic debugging I would at least switch to TwenySixteen and disable other plugins. – helgatheviking Jan 08 '16 at 19:18
  • Ok, thanks I leave it there. I switched to the twentytwelve theme (that's where the child is based on, and no other themes installed) and that makes no difference what so ever. I turned on the debug and log mode in the wp-config file and there is an interesting output. I hope you know what it means and maybe you can shed some light in it. It looks like it could have something to do with it. It's in the next comment – A3O Jan 09 '16 at 08:46
  • Deprecated: Non-static method WC_send_pdf::get_woocommerce_pdf_date() should not be called statically in /Users/adri/Documents/websites/htdocs/wp-content/plugins/woocommerce-pdf-invoice/classes/class-pdf-functions-class.php on line 190 Warning: Cannot modify header information - headers already sent by (output started at /Users/adri/Documents/websites/htdocs/wp-content/plugins/woocommerce-pdf-invoice/classes/class-pdf-functions-class.php:190) in /Users/adri/Documents/websites/htdocs/wp-includes/pluggable.php on line 1228 – A3O Jan 09 '16 at 08:46
  • Does it work if you disable that? Do you get any emails? Or just not the BCC? That error just means that `WC_send_pdf::get_woocommerce_pdf_date` was called but the method `get_woocommerce_pdf_data()` is not defined as `public static function get_woocommerce_pdf_data()`. – helgatheviking Jan 09 '16 at 13:06
  • It is caused by the WooCommerce PDF Invoice plugin. Deactivating plugins did not improve the outcome. I'm just not receiving the BCC. New order emails, order completed emails (when I'm the buyer myself) are received fine. When I move to the most basic situation (twenty twelve theme, no plugins loaded but woocommerce) there is still no bcc received. Just the "normal ones". I have tried to echo the $headers variable in the email template but it seems that it has no value at all. Wrapped in an if statement like this – A3O Jan 09 '16 at 16:40
  • Returns always "nothing to see". It looks like it gets overwritten or cleaned out before the email is sent. – A3O Jan 09 '16 at 16:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100249/discussion-between-helgatheviking-and-a3o). – helgatheviking Jan 09 '16 at 18:12
  • Thank you for your great help. It was very useful to debug this issue with your help along the line. It turns out that it was caused by wp_mail() or by the PHP mailer. I installed the Postman plugin (one of your favorits) and now all mails are send AND received. Thank you Helgatheviking.. you are a real Awesome Kathy and you can replace Thor without any troubles ;-) – A3O Jan 10 '16 at 17:04
  • Great! So if this answer does work for you, would you mind accepting it as the answer? – helgatheviking Jan 10 '16 at 17:31
  • Sure. How? There is no v mark to click like i have seen before. – A3O Jan 10 '16 at 19:33
  • Ha... after all that I guess I forgot it wasn't you who originally asked the question. – helgatheviking Jan 11 '16 at 00:02
0

Found a templorary fix, It's not a BCC or CC, but It seems I can add more than 1 recipient to the email by comma separating the addresses.

would still like to do BCC or CC as well though, if anyone knows how.

LukeD1uk
  • 242
  • 2
  • 14