Add bcc to new order form for woocommerce - wordpress

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

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).

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.

Related

using apply_filters send_new_site_email, why not working

I have a multisite with a lot of sites on it, registered almost daily. I want to stop the automatic email being send for a new site.
I
n the codex, i found this filter:
apply_filters( 'send_new_site_email', bool $send, WP_Site $site, WP_User $user )
The first argument, $send, can be set to false. Then the mail will not be send.
So I am using the following add_filter function:
function disable_email($send,$site,$user){
$send = false;
return $send;
}
add_filter( 'send_new_site_email', 'disable_email',10,3 );
Why does this not work?
I think you need to increase priority, may be then it will work try something
add_filter( 'send_new_site_email', 'disable_email',25,3 );

BCC all outgoing emails wordpress

So I want to BCC all outgoing emails from my wordpress site to two static, hard coded email addresses.
I went to the pluggable.php file and hard coded the BCC headers to the wp_mail() function like this:
function wp_mail( $to, $subject, $message, $headers = ['Bcc: example#mail.com', "bcc: maik#gmail.com"], $attachments = array() ) {
But nothing seems to be happening.
What am I missing?
Please do not edit core WordPress files!
Instead, use the relevant hook like wp_mail in your case.
Here's an example and this code would be added into the theme functions file, or you can add it as a Must Use plugin:
add_filter( 'wp_mail', 'my_wp_mail_args' );
function my_wp_mail_args( $args ) {
// Just replace the email addresses with the correct ones. And note that you
// don't have to add multiple Bcc: entries - just use one Bcc: with one or
// more email addresses separated by a comma - Bcc: <email>, <email>, ...
if ( is_array( $args['headers'] ) ) {
$args['headers'][] = 'Bcc: example#mail.com, maik#gmail.com';
} else {
$args['headers'] .= "Bcc: example#mail.com, maik#gmail.com\r\n";
}
return $args;
}
PS: If you added that as a Must Use plugin, don't forget to add the <?php at the top.
And BTW, just to explain the "nothing seems to be happening", it's because the $headers (fourth parameter) value there can be overridden when the function is called, e.g. wp_mail( 'foo#example.com', 'test', 'test', [ 'From: no-reply#example2.com' ] ) — the $headers is set, hence the default value is not used.
So I hope this answer helps, and keep in mind, never edit any core WordPress files! First, because in many WordPress functions (and templates as well) there are hooks that you can use to modify the function/template output, and secondly, your edits will be gone when WordPress is updated. 🙂

Additional order email based on custom customer field

I need to send a copy of the order email to a second email adress defined by a custom field.
I have installed the iconic custom account field plugin and created a custom field on the user (I works and I can edit it in admin).
https://iconicwp.com/blog/the-ultimate-guide-to-adding-custom-woocommerce-user-account-fields/
It's named 'iconic-register-email'
I have also implemented this https://wordpress.stackexchange.com/questions/92020/adding-a-second-email-address-to-a-completed-order-in-woocommerce solution to send an extra email. It works with a hard-coded email.
Now I need to combine the solutions and I just can't get it to work. I have appended the send email code at the bottom of the iconic plugin code.
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);
function mycustom_headers_filter_function( $headers, $object ) {
$sae = 'a-custom-email#gmail.com';
if ($object == 'customer_completed_order') {
$headers .= 'BCC: Name <' . $sae . '>' . "\r\n";
}
return $headers;
}
This code works, but I need to use iconic-register-email field.
Need to fetch the registred email adress on a user, so when that user makes an order an order copy is sent to that adress defined by iconic-register-email.
My knowledge about coding is very limited, please help. Last thing to add before I can go live.
Managed to solve it. Not sure if it's the most elegant solution, but it works.
As it's predefined functions and arrays, this is how I did it
function mycustom_headers_filter_function( $headers, $object ) {
$sae = iconic_get_userdata( get_current_user_id(), 'iconic-register-email' );
if ($object == 'new_order') {
$headers .= 'BCC: <' . $sae . '>' . "\r\n";
}
return $headers;
}

woocommerce_email_recipient_customer_completed_order hook not being invoked

There are a bunch of example of using this woocommerce hook woocommerce_email_recipient_customer_completed_order. So I added it to functions.php, and concatenated a couple example recipients as a test, but only the purchaser receives an email. It doesn't seem like this filter is getting invoked since if I turn on debugging and error_log(...) there's nothing in the log file.
Is there a reason this isn't working? I tried bumping the priority up to 99, but that doesn't work either. The site uses all the defaults and doesn't override any of the templates.
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'custom_woocommerce_add_email_recipient', 10, 2);
function custom_woocommerce_add_email_recipient($recipient, $order) {
$recipient = $recipient . ', foo#example.com, bar#example.com';
return $recipient;
}
As I learn more about Woocommerce it looks like this site only goes as far as processing so I had to use the customer_processing_order mail ID instead to get this to work (thanks to #LoicTheAztec for verifying the customer_completed_order hook works), and instead of using the woocommerce_email_recipient_customer_processing_order hook I used woocommerce_email_headers and checked the mail ID.
add_filter( 'woocommerce_email_headers', 'woocommerce_email_headers_add_recipient', 10, 3);
function woocommerce_email_headers_add_recipient($headers, $mail_id, $order) {
if($mail_id == 'customer_processing_order') {
$member = null;
$memberMail = null;
foreach($order->get_meta_data() as $item) {
if ($item->key === 'member_name') {
$member = $item->value;
$memberMail = $this->getMemberMail($member);
$headers .= "BCC: {$member} <{$member_mail}>\r\n";
break;
}
}
}
return $headers;
}

Wordpress duplicate comment detection

Does anyone know how to disable duplicate comment detection in Wordpress (2.9.2)? I'm looking for a way to do this programatically without editing core files. We're adding comments via XMLRPC and the duplicate detection in wp-includes/comment.php (line 494) is causing issues during testing.
Thanks!
Actually, you don't need to edit ANY core files to do this. Just put these one filter and two tiny functions in your theme's functions.php file and duplicate comments will no longer be rejected.
add_filter( 'wp_die_handler', 'my_wp_die_handler_function', 9 ); //9 means you can unhook the default before it fires
function my_wp_die_handler_function($function) {
return 'my_skip_dupes_function'; //use our "die" handler instead (where we won't die)
}
//check to make sure we're only filtering out die requests for the "Duplicate" error we care about
function my_skip_dupes_function( $message, $title, $args ) {
if (strpos( $message, 'Duplicate comment detected' ) === 0 ) { //make sure we only prevent death on the $dupe check
remove_filter( 'wp_die_handler', '_default_wp_die_handler' ); //don't die
}
return; //nothing will happen
}
Currently, there are no hooks available to do this without editing core files.
The best way would be to comment out the duplicate check from wp-includes/comment.php
$dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_approved != 'trash' AND ( comment_author = '$comment_author' ";
if ( $comment_author_email )
$dupe .= "OR comment_author_email = '$comment_author_email' ";
$dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
I had the same issue when replying in the backend on comments.
But just replying with the same comment on the frontend worked fine without changing anything.
Hope this might help someone.

Resources