Admin Dashboard, Emails

WooCommerce Change Sender “From” Name And Email Address Based On The Customer

Pre-Requisites

There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About This Solution

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) { ?>
<section class="ecommercehints_user_meta_fields">
<h2>Conditional WooCommerce email settings</h2>
<p>These fields determine the email sender "from" name and email address for this specific customer.</p>
    <table class="form-table">
        <tr>
            <th>
                <label for="email_from_name"><?php _e( '"From" name' ); ?></label>
            </th>
            <td>
                <input type="text" name="email_from_name" id="email_from_name" value="<?php echo esc_attr( get_the_author_meta( 'email_from_name', $user->ID ) ); ?>" class="regular-text" placeholder="<?php echo get_option( 'woocommerce_email_from_name' ) ?>"/>
				<p class="description">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 <a href="<?php echo admin_url() ?>admin.php?page=wc-settings&tab=email" target="_blank">WooCommerce email settings.</a></p>
            </td>
		</tr>
		<tr>
            <th>
                <label for="email_from_address"><?php _e( '"From" email address' ); ?></label>
            </th>
            <td>
                <input type="text" name="email_from_address" id="email_from_address" value="<?php echo esc_attr( get_the_author_meta( 'email_from_address', $user->ID ) ); ?>" class="regular-text" placeholder="<?php echo get_option( 'woocommerce_email_from_address' ) ?>"/>
				<p class="description">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 <a href="<?php echo admin_url() ?>admin.php?page=wc-settings&tab=email" target="_blank">WooCommerce email settings.</a></p>
            </td>
        </tr>
</table>
<h3>Select which emails to be affected</h3>
<table class="form-table">
		<tr>
            <th>
                <label for="new_order"><?php _e( 'New order' ); ?></label>
            </th>
            <td>
                <input type="checkbox" name="new_order" id="new_order" class="checkbox" <?php if(get_the_author_meta('new_order', $user->ID)=='on' ){ echo "checked"; } ?> />
            </td>
		</tr>
		<tr>
            <th>
                <label for="cancelled_order"><?php _e( 'Cancelled order' ); ?></label>
            </th>
            <td>
                <input type="checkbox" name="cancelled_order" id="cancelled_order" class="checkbox" <?php if(get_the_author_meta('cancelled_order', $user->ID)=='on' ){ echo "checked"; } ?> />
            </td>
		</tr>
		<tr>
            <th>
                <label for="failed_order"><?php _e( 'Failed order' ); ?></label>
            </th>
            <td>
                <input type="checkbox" name="failed_order" id="failed_order" class="checkbox" <?php if(get_the_author_meta('failed_order', $user->ID)=='on' ){ echo "checked"; } ?> />
            </td>
		</tr>
		<tr>
            <th>
                <label for="customer_on_hold_order"><?php _e( 'Order on-hold' ); ?></label>
            </th>
            <td>
                <input type="checkbox" name="customer_on_hold_order" id="customer_on_hold_order" class="checkbox" <?php if(get_the_author_meta('customer_on_hold_order', $user->ID)=='on' ){ echo "checked"; } ?> />
            </td>
		</tr>
		<tr>
            <th>
                <label for="customer_processing_order"><?php _e( 'Processing order' ); ?></label>
            </th>
            <td>
                <input type="checkbox" name="customer_processing_order" id="customer_processing_order" class="checkbox" <?php if(get_the_author_meta('customer_processing_order', $user->ID)=='on' ){ echo "checked"; } ?> />
            </td>
		</tr>
		<tr>
            <th>
                <label for="customer_completed_order"><?php _e( 'Completed order' ); ?></label>
            </th>
            <td>
                <input type="checkbox" name="customer_completed_order" id="customer_completed_order" class="checkbox" <?php if(get_the_author_meta('customer_completed_order', $user->ID)=='on' ){ echo "checked"; } ?> />
            </td>
		</tr>
		<tr>
            <th>
                <label for="customer_refunded_order"><?php _e( 'Refunded order' ); ?></label>
            </th>
            <td>
                <input type="checkbox" name="customer_refunded_order" id="customer_refunded_order" class="checkbox" <?php if(get_the_author_meta('customer_refunded_order', $user->ID)=='on' ){ echo "checked"; } ?> />
            </td>
		</tr>
		<tr>
            <th>
                <label for="customer_invoice"><?php _e( 'Customer invoice / Order details' ); ?></label>
            </th>
            <td>
                <input type="checkbox" name="customer_invoice" id="customer_invoice" class="checkbox" <?php if(get_the_author_meta('customer_invoice', $user->ID)=='on' ){ echo "checked"; } ?> />
            </td>
		</tr>
		<tr>
            <th>
                <label for="customer_note"><?php _e( 'Customer note' ); ?></label>
            </th>
            <td>
                <input type="checkbox" name="customer_note" id="customer_note" class="checkbox" <?php if(get_the_author_meta('customer_note', $user->ID)=='on' ){ echo "checked"; } ?> />
            </td>
		</tr>
		<tr>
            <th>
                <label for="customer_reset_password"><?php _e( 'Reset password' ); ?></label>
            </th>
            <td>
                <input type="checkbox" name="customer_reset_password" id="customer_reset_password" class="checkbox" <?php if(get_the_author_meta('customer_reset_password', $user->ID)=='on' ){ echo "checked"; } ?> />
            </td>
		</tr>
		<tr>
            <th>
                <label for="customer_new_account"><?php _e( 'New account' ); ?></label>
            </th>
            <td>
                <input type="checkbox" name="customer_new_account" id="customer_new_account" class="checkbox" <?php if(get_the_author_meta('customer_new_account', $user->ID)=='on' ){ echo "checked"; } ?> />
            </td>
		</tr>
    </table>
	<p>Generated by <a href="https://ecommercehints.com" target="_blank" rel="noopener">eCommerce Hints</a></p>
</section>
<?php
}

// Save "from" name if edited by admin
add_action( 'personal_options_update', 'ecommercehints_update_custom_meta_fields_field' );
add_action( 'edit_user_profile_update', 'ecommercehints_update_custom_meta_fields_field' );
function ecommercehints_update_custom_meta_fields_field( $user_id ) {
    if ( current_user_can( 'edit_user', $user_id ) ) {
        update_user_meta( $user_id, 'email_from_name', $_POST['email_from_name'] );
		update_user_meta( $user_id, 'email_from_address', $_POST['email_from_address'] );
		update_user_meta( $user_id, 'new_order', $_POST['new_order'] );
		update_user_meta( $user_id, 'cancelled_order', $_POST['cancelled_order'] );
		update_user_meta( $user_id, 'failed_order', $_POST['failed_order'] );
		update_user_meta( $user_id, 'customer_on_hold_order', $_POST['customer_on_hold_order'] );
		update_user_meta( $user_id, 'customer_processing_order', $_POST['customer_processing_order'] );
		update_user_meta( $user_id, 'customer_completed_order', $_POST['customer_completed_order'] );
		update_user_meta( $user_id, 'customer_refunded_order', $_POST['customer_refunded_order'] );
		update_user_meta( $user_id, 'customer_invoice', $_POST['customer_invoice'] );
		update_user_meta( $user_id, 'customer_note', $_POST['customer_note'] );
		update_user_meta( $user_id, 'customer_reset_password', $_POST['customer_reset_password'] );
		update_user_meta( $user_id, 'customer_new_account', $_POST['customer_new_account'] );
	}
}

// Change "from" name to the newly defined name
add_filter( 'woocommerce_email_from_name', 'ecommercehints_change_sender_email_name', 10, 2 );
function ecommercehints_change_sender_email_name( $from_name, $wc_email ) {
	global $current_user;
	$email_from_name = get_user_meta( $current_user->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;
}

Snippet Benefits

  • Provide a more personalised experience for your customers by changing the From Name and From Email Address to an account manager or sales representative.

100 WooCommerce Conversion Rate Optimisation Tips

This field is for validation purposes and should be left unchanged.

Let’s collaborate!

Need to outsource WordPress development?

Join forces with UnlimitedWP for an outsourced white label web development service you can truly rely on.

First Timer Here?

Sign up to receive 20% off on your first month with us.

26027
WELCOME OFFER