This code snippet allows customers to receive the WooCommerce emails from different people in your organisation.
Once implemented, new fields will be shown in the user editor.
You’ll notice you can change the “From Name” and “From Email Address” for the specific user being edited and the WooCommerce emails to be affected.
This only really works if your users have created an account because that is where the changes are made (and stored as user meta).
If you are using an SMTP plugin then use this at your own risk. It works perfectly fine on a default WooCommerce setup. You’ll need to look at your SMTP settings if the changes are not reflected.
What’s the use case for this snippet?
I manage a car sales website where sales representatives are assigned customers.
Sean and Mary, both sales representatives, asked that the WooCommerce order confirmation emails are sent to their assigned customers, from their email addresses.
So if Sean was my sales representative, and I bought a car, the WooCommerce order confirmation email would be from Sean and his email address.
If Mary was my sales representative, and I bought a car, the WooCommerce order confirmation email would be from Mary and her email address.
So I came up with this code snippet
Now, the car sales company can choose the “From Name”, the “From Email”, and the emails to be affected all from the user editor!
/**
* Snippet Name: WooCommerce Change Sender "From" Name And Email Address Based On The Customer
* Snippet Author: ecommercehints.com
*/
// Show email settings section and fields in user editor
add_action('show_user_profile', 'ecommercehints_custom_email_settings_fields');
add_action('edit_user_profile', 'ecommercehints_custom_email_settings_fields');
function ecommercehints_custom_email_settings_fields($user) { ?>
Conditional WooCommerce email settings
These fields determine the email sender "from" name and email address for this specific customer.
How the sender's name appears in WooCommerce emails for this specific customer. If left blank, the default "from" name will be used, set in the WooCommerce email settings.
How the sender's email address appears in WooCommerce emails for this specific customer. If left blank, the default "from" email address will be used, set in the WooCommerce email settings.
Select which emails to be affected
ID)=='on' ){ echo "checked"; } ?> />
ID)=='on' ){ echo "checked"; } ?> />
ID)=='on' ){ echo "checked"; } ?> />
ID)=='on' ){ echo "checked"; } ?> />
ID)=='on' ){ echo "checked"; } ?> />
ID)=='on' ){ echo "checked"; } ?> />
ID)=='on' ){ echo "checked"; } ?> />
ID)=='on' ){ echo "checked"; } ?> />
ID)=='on' ){ echo "checked"; } ?> />
ID)=='on' ){ echo "checked"; } ?> />
ID)=='on' ){ echo "checked"; } ?> />
Generated by eCommerce Hints
ID, 'email_from_name' , true );
$email_from_address = get_user_meta( $current_user->ID, 'email_from_address' , true );
$new_order = get_user_meta( $current_user->ID, 'new_order' , true );
$cancelled_order = get_user_meta( $current_user->ID, 'cancelled_order' , true );
$failed_order = get_user_meta( $current_user->ID, 'failed_order' , true );
$customer_on_hold_order = get_user_meta( $current_user->ID, 'customer_on_hold_order' , true );
$customer_processing_order = get_user_meta( $current_user->ID, 'customer_processing_order' , true );
$customer_completed_order = get_user_meta( $current_user->ID, 'customer_completed_order' , true );
$customer_refunded_order = get_user_meta( $current_user->ID, 'customer_refunded_order' , true );
$customer_invoice = get_user_meta( $current_user->ID, 'customer_invoice' , true );
$customer_note = get_user_meta( $current_user->ID, 'customer_note' , true );
$customer_reset_password = get_user_meta( $current_user->ID, 'customer_reset_password' , true );
$customer_new_account = get_user_meta( $current_user->ID, 'customer_new_account' , true );
if( $wc_email->id == 'customer_on_hold_order' && $customer_on_hold_order || $wc_email->id == 'new_order' && $new_order || $wc_email->id == 'cancelled_order' && $cancelled_order || $wc_email->id == 'failed_order' && $failed_order || $wc_email->id == 'customer_processing_order' && $customer_processing_order || $wc_email->id == 'customer_completed_order' && $customer_completed_order || $wc_email->id == 'customer_refunded_order' && $customer_refunded_order || $wc_email->id == 'customer_invoice' && $customer_invoice || $wc_email->id == 'customer_note' && $customer_note || $wc_email->id == 'customer_reset_password' && $customer_reset_password || $wc_email->id == 'customer_new_account' && $customer_new_account ) {
if (!empty(get_user_meta( $current_user->ID, 'email_from_name' , true ))){ // Show default from name if custom fields are blank
$from_name = get_user_meta( $current_user->ID, 'email_from_name' , true );
}
}
return $from_name;
}
// Change "from" email to the newly defined name
add_filter( 'woocommerce_email_from_address', 'ecommercehints_change_sender_email_address', 10 ,2 );
function ecommercehints_change_sender_email_address ( $from_email, $wc_email ){
global $current_user;
$email_from_name = get_user_meta( $current_user->ID, 'email_from_address' , true );
$email_from_address = get_user_meta( $current_user->ID, 'email_from_address' , true );
$new_order = get_user_meta( $current_user->ID, 'new_order' , true );
$cancelled_order = get_user_meta( $current_user->ID, 'cancelled_order' , true );
$failed_order = get_user_meta( $current_user->ID, 'failed_order' , true );
$customer_on_hold_order = get_user_meta( $current_user->ID, 'customer_on_hold_order' , true );
$customer_processing_order = get_user_meta( $current_user->ID, 'customer_processing_order' , true );
$customer_completed_order = get_user_meta( $current_user->ID, 'customer_completed_order' , true );
$customer_refunded_order = get_user_meta( $current_user->ID, 'customer_refunded_order' , true );
$customer_invoice = get_user_meta( $current_user->ID, 'customer_invoice' , true );
$customer_note = get_user_meta( $current_user->ID, 'customer_note' , true );
$customer_reset_password = get_user_meta( $current_user->ID, 'customer_reset_password' , true );
$customer_new_account = get_user_meta( $current_user->ID, 'customer_new_account' , true );
if( $wc_email->id == 'customer_on_hold_order' && $customer_on_hold_order || $wc_email->id == 'new_order' && $new_order || $wc_email->id == 'cancelled_order' && $cancelled_order || $wc_email->id == 'failed_order' && $failed_order || $wc_email->id == 'customer_processing_order' && $customer_processing_order || $wc_email->id == 'customer_completed_order' && $customer_completed_order || $wc_email->id == 'customer_refunded_order' && $customer_refunded_order || $wc_email->id == 'customer_invoice' && $customer_invoice || $wc_email->id == 'customer_note' && $customer_note || $wc_email->id == 'customer_reset_password' && $customer_reset_password || $wc_email->id == 'customer_new_account' && $customer_new_account ) {
if (!empty(get_user_meta( $current_user->ID, 'email_from_address' , true ))){ // Show default from address if custom fields are blank
$from_email = get_user_meta( $current_user->ID, 'email_from_address' , true );
}
}
return $from_email;
}
// Show email sender in a custom column when viewing users
add_action('manage_users_columns','ecommercehints_custom_user_column');
function ecommercehints_custom_user_column($column_headers) {
$column_headers['woocommerce_email_sender'] = __('WooCommerce Email Sender');
return $column_headers;
}
// Populate the column with the saved "from" sender
add_filter('manage_users_custom_column', 'ecommercehints_get_saved_value', 10, 3);
function add_user_column_value( $value, $column_name, $user_id ){
if ( 'woocommerce_email_sender' == $column_name ){
if( get_user_meta( $user_id, 'email_from_address', true ) ){
$value = get_user_meta( $user_id, 'email_from_name', true ) . ' (' . get_user_meta( $user_id, 'email_from_address', true ) . ')';
} else {
$value = get_option( 'woocommerce_email_from_name' ) . ' (' . get_option( 'woocommerce_email_from_address' ) . ')';
}
}
return $value;
}