WooCommerce: Get Customer ID From An Email Address

Customizing your WooCommerce store via PHP can involve a variety of tasks, from personalizing orders to managing customer interactions. Often, you might need to find a specific customer’s information, but all you have is their email address.

For example, on this same website, I have custom contact forms that give me a name and an email address upon submit. What if I need to check if the email address is an existing WooCommerce customer who has placed some orders?

Well, the PHP below will give you a quick way to “calculate” the customer ID if you only have an email address. It’s then easy to use the result in a custom calculation or core function, such as wc_get_orders().

Enjoy!

PHP Snippet: Calculate WooCommerce Customer ID (WordPress User ID) From An Email Address

/**
 * @snippet       Get WooCommerce User Id From User Email
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

function bbloomer_get_customer_id_from_email( $email ) {	
	$customer = get_user_by( 'email', $email );
	if ( $customer ) return $customer->ID;
	return false;
}

The bbloomer_get_customer_id_from_email() function retrieves the customer ID associated with a given email address. This can be useful in various situations within a WooCommerce environment.

Assuming you have the customer’s email address stored in a variable:

$customer_email = "customer@example.com";

…you can call the function to get the customer ID and run code based on the response:

$customer_id = bbloomer_get_customer_id_from_email( $customer_email );
if ( $customer_id ) {
  // Customer ID found
  // do something
} else {
  // Customer ID NOT found
  // do something else
}  

The function uses get_user_by( ’email’, $email ) to check if a customer exists with the provided email address. This function is part of WordPress and retrieves the WP_User object if a user with that email is found.

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Add Column to “Users” Admin Table
    On the admin side, you might need to display WooCommerce information inside the users table (WordPress Dashboard > Users). For example, their billing country. Or their phone number. Or maybe some custom calculation e.g. the number of completed orders. Either way, this is super easy. First, we add a new column – second, we tell […]
  • WooCommerce: Check if User Has Bought Product in the Last 365 Days
    A few snippets ago we introduced the magic WooCommerce inbuilt function “wc_customer_bought_product” – automatically, with a single line of PHP, you can find out if the user has already purchased a product ID. But when building my new #BloomerArmada section, I had to know if a user purchased a product ID in the last 365 […]
  • WooCommerce: Update User Meta After a Successful Order
    When an order is placed in WooCommerce, you might want to change/add something in the User Meta programmatically. For example, you could “check” a custom checkbox in the User Profile. Or maybe assign the User Twitter username. And so on ๐Ÿ™‚
  • WooCommerce: Display & Save WP User Profile Field @ Checkout
    I’m curious to know how many had the same problem. On the WooCommerce Checkout page, some user fields such as billing_name, shipping_address_1, etc. are automatically saved into the “WordPress User Profile” data upon processing. But what if we also wanted to display and save another existing user field, such as “user_twitter“, or “user_url“, which you […]
  • WooCommerce: Get List Of All Customers
    Today’s snippet is a helpful shortcut for getting the list of customers in your WooCommerce website. This may be necessary during customization, especially if you need tailor-made features for administrators and shop managers in the backend or frontend. How did I find out about the solution below? Well, our job is mainly copy/paste from online […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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