Optional Accounts Email Address in WooCommerce

Learn how to add an accounts email address for your customers’ orders & invoices in WooCommerce, without using a plugin. We’ll use a small bit of code and a custom field to add an “accounts email address” option for your customers. Then we’ll hook WooCommerce to CC order/invoice emails to your users’ accounts email address (if specified).

Before starting, you’ll need to make sure you’re using a custom child theme, and that you’ve installed the free Advanced Custom Fields plugin. You don’t need any programming experience – we’re just going to copy-and-paste a bit of code into a new file and do a bit of configuration magic.

Create a Custom Field

Advanced Custom Fields is an almost ubiquitous WordPress plugin. It’s always one of the first plugins I install when starting a new WordPress project. Here, we’re going to use it to create a new field against all Users who are in the “Customer” role.

In your website’s admin area, locate the Custom Fields menu option on the left and click on “Add New”. This sets up a new Field Group for us. Our field group is only going to have one field in it, but we can add more custom User/Customer fields into the field group later if we want to.

You need to give the new field group a name (something like “Customer Fields“), and then add a new field. Now, when you click on Add Field you get a big array of options thrown at you, but it’s not as complex as it seems:

  • Label: Accounts Email Address
  • Name: This should auto-populate with accounts_email_address
  • Type: Email
  • Instructions: “Optional additional email address to send orders & invoices to.”
  • Required: No

That’s enough to get the field in there for us.

Now we just need to set a couple of options to finish configuring the Field Group itself. In the Location box we can specify where (in the admin area) our Customer Fields should be shown. We only want it to be shown if we’re seeing a form where User Role is a Customer. Set this up in the Location box.

Custom ACF field, user role
Where should our Customer Fields be displayed in the Admin area.

To finish, just “Publish” the field group.

Now you can save an alternative “accounts” email address against your customers. Check it out by going to your site’s Users list. Find someone who’s a Customer, Edit them and you should see the new Accounts Email Address field in there.

Custom ACF Field
Our custom accounts email address field

Let’s Write some Code

Now we can store an optional Accounts Email Address against a user/customer, we need to be able to do something with it. To do this we’re going to make a new PHP file in our custom child theme. Then we’ll reference this new code file from our child theme’s functions.php file.

Create a new file in your custom child theme called functions-accounts-email.php and paste the following.

<?php

/**
 * CC order/invoice emails to a secondary accounts email address,
 * if a user has requested it.
 *
 * To use this, just create a custom field against a user called
 * 'accounts_email_address' and put a vlaid email address in there.
 */

// Block direct access.
if (!defined('WPINC')) {
    exit('Do NOT access this file directly.');
}

function custom_customer_invoice_recipients($recipient, $order) {
	if (!method_exists($order, 'get_user')) {
		// Not an order.
	} elseif (empty($user = $order->get_user())) {
		// The order doesn't have a user.
	} elseif (!is_a($user, 'WP_User')) {
		// $user isn't really a WP_User.
	} elseif (empty($user_id = $user->ID)) {
		// Sanity check. Invalid user id.
	} elseif (empty($accounts_email = sanitize_email((string) get_user_meta($user_id, 'accounts_email_address', true)))) {
		// accounts_email_address hasn't been specified for this user.
	} else {
		// If there's already a recipient specified (there should be)
		// then append a comma+space separator.
		if (!empty($recipient)) {
			$recipient .= ', ';
		}

		$recipient .= $accounts_email;
	}

	return $recipient;
}

add_filter(
	'woocommerce_email_recipient_customer_invoice',
	'custom_customer_invoice_recipients',
	10,
	2
);

We won’t go into detail here, but to summarise…

  • Intercept the WooCommerce filter, custom_customer_invoice_recipients
  • Does the order have a user associated with it?
    • Yes. Does the user have something in accounts_email_address?
      • Yes. Append the contents of accounts_email_address to the recipient list
  • Return the (possibly modified) recipient list

The final thing we need to do is reference this new bit of code. Open your custom child theme’s functions.php file and paste this into it.

// Copy order/invoice emails to accounts email address, if specified.
require_once dirname(__FILE__) . '/functions-accounts-email.php';

That’s all there is to it. You’ll want to test this by creating a test user, setting an account’s email address, and then making a dummy purchase. We’re pretty safe here… the logic is nice and straightforward – it’s not a big complicated lump that’s going to bring down your website if something goes wrong.

If you want to do more with WooCommerce’s outgoing emails, have a search for the following filters you can also hook into:

  • woocommerce_email_after_order_table
  • woocommerce_email_before_order_table
  • woocommerce_email_customer_details
  • woocommerce_email_footer
  • woocommerce_email_header
  • woocommerce_email_order_details

…don’t be afraid to experiment! 🙂

2 thoughts on “Optional Accounts Email Address in WooCommerce”

  1. I did all the thing that are in this tutorial but it is not working.
    in my user there is an additional field and there is his another email filled in, i made a functions-accounts-email.php and stored it in my child menu and i altered the functions.php but…… no succes

    woocommerce 5.8.0
    wordpress 5.8.1

    what did i do wrong? please help me

    Alwin Dietrich

    1. Hi Alwin

      This tutorial should work fine – I use it on a client website.

      In your website back-end, open a WooCommerce order that belongs to the user/customer, then go to “Order actions”, select “Email invoice / order details to customer” and then click the > button. Does that work? If it does not work then I would be happy to log in to your site for you and see if there is an error? No problem.

      Paul

Leave a Comment

Your email address will not be published. Required fields are marked *