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;
}
Related
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. 🙂
Is there any mail-tags to use?
for instance; for tracking client's ip we can use; [_remote_ip]
First step is to create an API key. To get an API key we have to register in the site IPInfoDB.
Once API key is ready we have to download the file ip2locationlite.class.php from the site IPInfoDB.
Next step is to create our custom plugin.I named it as "country_custom_plugin.php". Its always good to create a custom plugin inside a folder, so that all the required files for the corresponding plugin stays in the folder. Named the folder as "country_custom_plugin"
Move the file "ip2locationlite.class.php" to the folder "country_custom_plugin".
/*Calling the function from contact-form-7 module and passing the result of the function stylus_ip_location_get_cc */
add_filter( 'wpcf7_special_mail_tags', 'country_custom_ip_location', 10, 2 );
/*Function to get location of an user from the ip address*/
function country_custom_ip_location( $output, $name ){
/*including the third party integration to get IP Location*/
include_once('ip2locationlite.class.php');
/*Special tag values are passed in format wpcf7.$name which we convert to _$name*/
$name = preg_replace( '/^wpcf7\./', '_', $name );
/*If location is requested in contact form enter the loop*/
if ( '_custom_ip_location' == $name ) {
$ipLite = new ip2location_lite;
/*Entering the API key value generated*/
$ipLite->setKey('"Enter your API Key Here"');
/*Getting the IP address*/
$ipaddress = preg_replace( '/[^0-9a-f.:, ]/', '', $_SERVER['REMOTE_ADDR'] );
/*Getting the Location*/
$visitorGeolocation = $ipLite->getCity($ipaddress);
if (!empty($visitorGeolocation) && is_array($visitorGeolocation)) {
$output = $visitorGeolocation['regionName'] . ', ' . $visitorGeolocation['countryName'] . ', ' . $visitorGeolocation['countryCode'];
}
}
return $output;
}
Reference.Hope this will help. Please let me know if any issue.
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;
}
I've created a form with contact form 7, and I need to send two e-mails, one with all the data that I pick up from the form, another one with a "thank you" message and other important information, these informations are statics, all the same for every user that clicks in the submit button.
So my problem is this email, CF7 can send only one type of mail to multiple users, but not two differents mail to two differents users. The second mail needs to use the [your-mail] (the mail that the users write in the form).
I've discovered the on_sent_ok function that allows me to open a page or something else after the submit, but I have no idea how to send this different mail.
It's been a while from your request, but I write here my solution for future needs.
You can reach the goal by following 2 steps:
A) handle the sent mail Contact Form 7 event.
function your_wpcf7_mail_sent_function( $contact_form ) {
$title = $contact_form->title;
$submission = WPCF7_Submission::get_instance();
if ($submission && "New hospital" == $title) {
$posted_data = $submission->get_posted_data();
//$posted_data is an associative array that contains all the form input values,
//so you can use it to proceed with B step
}
}
add_action( 'wpcf7_mail_sent', 'wpcf7_mail_sent_event_triggered' );
If you're not sure on where place this code my suggestion is to add it at the beginning of your theme's function.php file.
B) send a custom mail using wp_mail API.
If you take a look at the docs you'll find that the API is quite easy to understand.
$to = 'sendto#example.com';
$subject = 'The subject';
$body = 'The email body content';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
Obviously you can customize the body message using or not the $posted_data values according to your needs.
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.