How to change email sender address and name in WooCommerce for specific email notifications?
For example:
Change the sender name and email address just for customer processing order email notifications.
But not for all email notifications, just for specific ones.
The sender name and email address are set here (at the end of Woocommerce "Emails" setting tab:
This fields are passed through dedicated filters hook that allow you to change conditionally the values.
Here is an example conditionally restricted to "customer processing email notification":
// Change sender name
add_filter( 'woocommerce_email_from_name', function( $from_name, $wc_email ){
if( $wc_email->id == 'customer_processing_order' )
$from_name = 'Jack the Ripper';
return $from_name;
}, 10, 2 );
// Change sender adress
add_filter( 'woocommerce_email_from_address', function( $from_email, $wc_email ){
if( $wc_email->id == 'customer_processing_order' )
$from_email = 'jack.the.ripper#freek.com';
return $from_email;
}, 10, 2 );
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
Some other WC_Email Ids that you can use in your condition:
- 'customer_completed_order'
- 'customer_on_hold_order'
- 'customer_refunded_order'
- 'customer_new_account'
- 'new_order' ( admin notification )
- 'cancelled_order' ( admin notification )
- 'failed_order' ( admin notification )
Related
So this is more of my solution here but I wanted to open this up to see if the community could find a better alternative or potentially find use for this solve.
Our client asked us for the following alteration to the email order receipts received on order creation: The receipts should go to the account holder and cc the billing email if it's different
As we know Woocommerce by default sends the order receipt (Customer Processing) based on only the set billing_email on checkout so I started to look for a way to add on the tack on the account owner email to it as well.
I did some digging and found a few answers on Stackoverflow on how to do this and the proposed solution utilized the woocommerce_email_recipient_customer_processing_order built-in function. This approach would only add on the email in the "to" header -less than ideal. It also doesn't account for potential duplicate sends to the same email address which, at least in the case of our server here, caused the email to not be delivered. No bueno.
The function below represents a work around this wherein we're calling upon the WP Core function wp_get_current_user() to obtain the email that the user is associated with and then checking whether it's the same as the billing email.
add_filter( 'woocommerce_email_headers', 'add_email_recipient', 10, 3);
function add_email_recipient($header, $email_id, $order) {
// Only for "Customer Processing Emails" email notifications
if( ! ( 'customer_processing_order' == $email_id ) ) return;
$curr_user = wp_get_current_user();
$account_holder_email = $curr_user->user_email;
$billing_email = $order->get_billing_email();
$header ='';
if ( $account_holder_email != $billing_email ) {
$header .= 'Cc: '.$account_holder_email;
}
return $header;
}
The logic intends to flow the following way:
Adjust the woocommerce email headers
If the email is the "customer_processing_order" continue
Get the current user email
Get the billing email set in the order
assign a CC field of the current user email
done
As far as I could tell there wasn't an easier way to handle this so I'm posting this up here in the hopes to see if someone else has a more elegant solution. The above code works by placing in the child theme functions.php.
You can't get the current user or the current user ID on email notification hooks.
You need first to get the customer ID from the order, then you can get the WP_User object to get the account email.
There is 2 different ways to add the customer account email when it's different from the order billing email in Customer processing order email notification:
1) Add the customer account email as an additional recipient:
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'add_customer_processing_order_email_recipient', 10, 2 );
function add_customer_processing_order_email_recipient( $recipient, $order ) {
// Not in backend (avoiding errors)
if( is_admin() ) return $recipient;
if( $order->get_customer_id() > 0 ){
// Get the customer WP_User object
$wp_user = new WP_User($order->get_customer_id());
if ( $wp_user->user_email != $order->get_billing_email() ) {
// Add account user email to existing recipient
$recipient .= ','.$wp_user->user_email;
}
}
return $recipient;
}
Code goes in function.php file of your active child theme (active theme). It should works.
2) Add the customer account email as a CC email address:
add_filter( 'woocommerce_email_headers', 'add_cc_email_to_headers', 10, 3);
function add_cc_email_to_headers($header, $email_id, $order) {
// Only for "Customer Processing Emails" email notifications
if( 'customer_processing_order' == $email_id ) {
if( $order->get_customer_id() > 0 ){
// Get the customer WP_User object
$wp_user = new WP_User($order->get_customer_id());
if ( $wp_user->user_email != $order->get_billing_email() ) {
$header .= 'Cc: ' . utf8_decode($order->get_formatted_billing_full_name() . ' <' . $wp_user->user_email . '>') . "\r\n";
}
}
}
return $header;
}
Code goes in function.php file of your active child theme (active theme). It should works.
In Woocommerce, I would like to add a email custom field with the Advanced Custom Fields plugin to products post type.
If customer place an order I would like to add the corresponding email adresses for each order items to new order email notification .
How to add recipients from product custom fields to Woocommerce new order email notification?
For "New order" email notification, here is the way to add custom emails from items in the order (The email custom field is set with ACF in the products).
So you will set first a custom field in ACF for "product" post type:
Then you will have in backend product edit pages:
Once done and when that product custom-fields will be all set with an email adress, you will use this code that will add to "New order" notification the emails for each corresponding item in the order:
add_filter( 'woocommerce_email_recipient_new_order', 'add_item_email_to_recipient', 10, 2 );
function add_item_email_to_recipient( $recipient, $order ) {
if( is_admin() ) return $recipient;
$emails = array();
// Loop though Order IDs
foreach( $order->get_items() as $item_id => $item ){
// Get the student email
$email = get_field( 'product_email', $item->get_product_id() );
if( ! empty($email) )
$emails[] = $email; // Add email to the array
}
// If any student email exist we add it
if( count($emails) > 0 ){
// Remove duplicates (if there is any)
$emails = array_unique($emails);
// Add the emails to existing recipients
$recipient .= ',' . implode( ',', $emails );
}
return $recipient;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related: Send Woocommerce Order to email address listed on product page
We have a "main wholesaler" who is in charge of other wholesalers. This wholesaler is assigned to user role: wholesale_customer just as the other wholesalers.
This "main wholesaler" places orders for his stores, and the other wholesalers place orders for their own stores as normal.
I would like that the "main wholesaler" receive an email notification when the other wholesalers place their orders, so he will get notified when other wholesalers are ordering because he gets a commission.
So is there a way that we can make the "main wholesaler" to receive an email notification when the other wholesalers place their orders?
Maybe this can be done with a function somewhat like this:
WooCommerce email notifications: different email recipient for different cities
Any help is appreciated.
Updated (solved a bug in backend email settings)
The following function will send an additional "new order" email notidfication to the "Wholesale manager" when others wholesale_customer user roles will make a purchase.
You will just need to set in this function the User ID for the "Wholesale manager"…
add_filter( 'woocommerce_email_recipient_new_order', 'wholesale_manager_email_recipient', 20, 2 );
function wholesale_manager_email_recipient( $recipient, $order ) {
if( is_admin() ) return $recipient;
// Only for 'wholesale_customer' user role
if( is_admin() || ! user_can( $order->get_user_id(), 'wholesale_customer' ) )
return $recipient; // Exit if not a 'wholesale_customer'
// HERE Set the main wholesale manager user ID
$manager_id = 5;
$manager_user = new WP_User( $manager_id );
$manager_email = $manager_user->user_email;
// Add manager email when other 'wholesale_customer' place orders
if ( $order->get_user_id() != $manager_id )
$recipient .= ',' . $manager_email; // coma separate emails
return $recipient;
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
I'm using Woocommerce and the Product Addons plugin to add extra fields to a product. One of those fields is an email address to let people send the confirmation of the order to a DIFFERENT address than the billing address shown on the checkout page. Emails should be sent to both addresses.
Any thoughts on maybe how to modify the functions.php file to do this?
In the woocommerce_email_recipient_{$this->id} filter hook, you can use the $order argument to get your 2nd email.
But first Lets add globally an email field with the Product Add-ons plugin…
The add on field on the product (fill the field and add to cart):
This "Email" field in order-received (Thank you) page, after checkout:
As you can notice the label of this field is "Email"…
Now if I look in the database in wp_woocommerce_order_itemmeta for this order I can see for the meta_key "Email" the meta_value "loic#TheAztec.com" :
Now I can set the correct meta_key in the code below to get my email.
Here is the code that will add this additional email recipient for processing and completed customer order email notifications:
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'additional_customer_email_recipient', 10, 2 ); // Processing Order
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'additional_customer_email_recipient', 10, 2 ); // Completed Order
function additional_customer_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
$additional_recipients = array(); // Initializing…
// Iterating though each order item
foreach( $order->get_items() as $item_id => $item_data ){
// HERE set the the correct meta_key (like 'Email') to get the correct value
$email = wc_get_order_item_meta( $item_id, 'Email', true );
// Avoiding duplicates (if many items with many emails)
// or an existing email in the recipient
if( ! in_array( $email, $additional_recipients ) && strpos( $recipient, $email ) === false )
$additional_recipients[] = $email;
}
// Convert the array in a coma separated string
$additional_recipients = implode( ',', $additional_recipients);
// If an additional recipient exist, we add it
if( count($additional_recipients) > 0)
$recipient .= ','.$additional_recipients;
return $recipient;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works.
You can add below code in your function.php
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'your_email_recipient_filter_function', 10, 2);
function your_email_recipient_filter_function($recipient, $object) {
$recipient = $recipient . ', me#myemail.com';
return $recipient;
}
and if you want to send email in BCC then please try below code:
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);
function mycustom_headers_filter_function( $headers, $object ) {
if ($object == 'customer_completed_order') {
$headers .= 'BCC: My name <my#email.com>' . "\r\n";
}
When the order status in WooCommerce is set from pending to processing I can send an email to my customer. But I need to send an email only to a custom email adresse instead of the customer default address.
Is there a hook or filter for the functions.php file?
This custom function hooked in woocommerce_email_recipient_customer_processing_order filter hook, will do the job (Set in it your replacement email):
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'processing_order_replacement_email_recipient', 10, 2 );
function processing_order_replacement_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Set HERE your replacement recipient email(s)… (If multiple, separate them by a coma)
$recipient = 'name#example.com';
return $recipient;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works
To add the custom recipient to the customer email, you should use this instead (if needed):
$recipient .= ',name#example.com';