I using Woocommerce and actually I receive order notifications only to one email. I would like to receive notifications about orders in 2 different emails depending on customer location:
For customer from zone 1 (Germany) I would like to receive the email notifications at Mail #1 (mail1#mail.com),
For all other zones like zone 2 (Mexico) I would like to receive the email notifications at Mail #2 (mail2#mail.com).
I looking for some functions on net, but I was find only funtcions to send to two email adresses, but without any If condition.
What I will need is something like that:
if ($user->city == 'Germany') $email->send('mail1#mail.com')
else $email->send('mail2#mail.com')
Which hook can I use to get this working?
Thanks.
You can use a custom function hooked in woocommerce_email_recipient_{$this->id} filter hook, targeting 'New Order' email notification, this way:
add_filter( 'woocommerce_email_recipient_new_order', 'diff_recipients_email_notifications', 10, 2 );
function diff_recipients_email_notifications( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Set HERE your email adresses
$email_zone1 = 'name1#domain.com';
$email_zone_others = 'name2#domain.com';
// Set here your targeted country code for Zone 1
$country_zone1 = 'GE'; // Germany country code here
// User Country (We get the billing country if shipping country is not available)
$user_country = $order->shipping_country;
if(empty($user_shipping_country))
$user_country = $order->billing_country;
// Conditionaly send additional email based on billing customer city
if ( $country_zone1 == $user_country )
$recipient = $email_zone1;
else
$recipient = $email_zone_others;
return $recipient;
}
For WooCommerce 3+, some new methods are required and available from WC_Order class concerning billing country and shipping country: get_billing_country() and get_shipping_country() …
Usage with $order instance object:
$order->get_billing_country(); // instead of $order->billing_country;
$order->get_shipping_country(); // instead of $order->shipping_country;
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested and works.
Related answers:
How to get order ID in woocommerce_email_headers hook
Send on-hold order status email notification to admin
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.
I am using Wordpress 4.9.6 and WooCommerce version 3.4.3 and I need to send 'Order on Hold' email for a specific shipping method.
Reason?
I use DHL shipping plugin to calculate shipping and an 'Alternate' shipping method is also available. If the user chooses DHL shipping while checking out, the shipping cost is calculated and the order is good to go. However, if they choose the 'Alternate' shipping method, I got to inform them that their order is on hold till they pay for shipping because the 'Alternate' method is 'Free Shipping' renamed and I will issue a separate invoice for them to pay for shipping once they have ordered.
Searching for a solution to my problem, I have found some code that matches my needs in this answer thread: Customizing Woocommerce New Order email notification based on shipping method
But I am unable to figure out how to edit this code in order to make it work for my specific scenario.
Your help is greatly appreciated.
To make it work for a renamed free shipping method, you will need to change the code a bit:
add_action ('woocommerce_email_order_details', 'custom_email_notification_for_shipping', 5, 4);
function custom_email_notification_for_shipping( $order, $sent_to_admin, $plain_text, $email ){
// Only for "On hold" email notification and "Free Shipping" Shipping Method
if ( 'customer_on_hold_order' == $email->id && $order->has_shipping_method('free_shipping') ){
$order_id = $order->get_id(); // The Order ID
// Your message output
echo "<h2>Shipping notice</h2>
<p>Your custom message goes here… your custom message goes here… your custom message goes here… your custom message goes here… your custom message goes here…</p>";
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
Forcing "On hold" and "Completed" email notification (optional)
On orders status change the below code will trigger "On hold" email notification only for your renamed "Free shipping" shipping method and "Completed" email notification.
add_action( 'woocommerce_order_status_changed', 'sending_on_hold_email_notification', 20, 4 );
function sending_on_hold_email_notification( $order_id, $old_status, $new_status, $order ){
// Only "On hold" order status and "Free Shipping" Shipping Method
if ( $order->has_shipping_method('free_shipping') && $new_status == 'on-hold' ){
// Getting all WC_emails objects
$notifications = WC()->mailer()->get_emails();
// Send "On hold" email notification
$notifications['WC_Email_Customer_On_Hold_Order']->trigger( $order_id );
} elseif ( ! $order->has_shipping_method('free_shipping') && $new_status == 'completed' ){
// Getting all WC_emails objects
$notifications = WC()->mailer()->get_emails();
// Send "On hold" email notification
$notifications['WC_Email_Customer_Completed_Order']->trigger( $order_id );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
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'd like to indicate in WooCommerce "new order" email notification, if it's a repeat customer.
It seems simple, but I've tried about 5 different methods and none worked. I've tried putting this into 2 different hooks:
woocommerce_email_after_order_table
woocommerce_email_subject_new_order.
Seems like wc_get_customer_order_count($user->ID) should work but it appears that the $user object is not passed into those hook's functions, right?
I'm also wondering if this is possible when it's a guest and not a registered user, maybe by comparing the email address?
Thanks
WooCommerce Email notifications are related to orders.
In woocommerce_email_after_order_table hook, you have the Order object as an argument in your hooked custom function and also the $email object.
With that $order object, you can get the user ID this way:
$user_id = $user_id = $order->get_user_id();
From the $email object you can target the new order email notification.
So the working code is going to be:
add_action( 'woocommerce_email_after_order_table', 'customer_order_count', 10, 4);
function customer_order_count( $order, $sent_to_admin, $plain_text, $email ){
if ( $order->get_user_id() > 0 ){
// Targetting new orders (that will be sent to customer and to shop manager)
if ( 'new_order' == $email->id ){
// Getting the user ID
$user_id = $order->get_user_id();
// Get the user order count
$order_count = wc_get_customer_order_count( $user_id );
// Display the user order count
echo '<p>Customer order count: '.$order_count.'</p>';
}
}
}
You can also use instead the woocommerce_email_before_order_table hook for example…
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.
I am very new to WordPress, and have created an e-commerce store with WooCommerce.
After the customer places an order, I get an email and the customer get an email- one for me to say what they have ordered, and one to them as a thank you email.
Within this thank you email, in my functions.php file, I have learned to change the subject of the header to include their name such as this:
//add the first name of the person to the person getting the reciept in the subject of the email.
add_filter('woocommerce_email_subject_customer_processing_order','UEBC_change_processing_email_subject', 10, 2);
function UEBC_change_processing_email_subject( $subject, $order ) {
global $woocommerce;
$subject = 'Thanks for your ' . get_bloginfo( 'name', 'display' ) . ' Order, '.$order->billing_first_name .'!';
return $subject;
}
The code snippet above works correctly, and is only displayed to the customer, not to me. e.g. "thanks for your order ABCD Clothes order John!".
Within the body of the email, I am trying to make this personal as a small thank you message, however, when I make the message, I am using the hook:
add_action( 'woocommerce_email_before_order_table', 'custom_add_content', 20,1 );
I know that since im using the woocommerce_email_before_order_table hook, the custom function will be send in the body of the email to both the customer and myself.
I was wondering, is there a hook that Woocommerce provides so that the custom function will only be sent to the customer within the body of the email?
For example: woocommerce_email_header_customer_processing_order or words to that effect?
Thanks
To add some custom content using woocommerce_email_before_order_table hook and targeting just the customer "processing order" email notification, you should try this:
add_action( 'woocommerce_email_before_order_table', 'custom_content_to_processing_customer_email', 10, 4 );
function custom_content_to_processing_customer_email( $order, $sent_to_admin, $plain_text, $email ) {
if( 'customer_processing_order' == $email->id ){
// Set here as you want your custom content (for customers and email notification related to processing orders only)
echo '<p class="some-class">Here goes your custom content… </p>';
}
}
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
This code is tested and works