When customizing WooCommerce functionality using PHP, we often need to retrieve various pieces of information about a customer. However, in some situations, the customer’s email address might be the only data point you have available.
For example, you might have a contact form that requires customers to provide their email address. After the user submits the form, what if you need to check if that email address corresponds to an existing WooCommerce customer who has already placed an order?
If you only have the email address, the following PHP snippet provides a quick way to “fetch” the customer ID. Once you have the ID, you can easily retrieve various customer details using custom functions (such as wc_get_orders()).

Code to Get User ID by Email
function wprs_get_customer_id_from_email( $email ) {
$customer = get_user_by( 'email', $email );
if ( $customer ) return $customer->ID;
return false;
}
The wprs_get_customer_id_from_email() function is designed to retrieve a customer’s ID via a specified email address. This is extremely useful in various scenarios within the WooCommerce environment.
Suppose we have stored the customer’s email address in a variable:
$customer_email = "customer@example.com";
Then, we can call the function to retrieve the customer ID and proceed based on the return value:
$customer_id = wprs_get_customer_id_from_email( $customer_email );
if ( $customer_id ) {
// Successfully retrieved user ID
} else {
// User ID not found
}
This function utilizes get_user_by( ’email’, $email ) to check if the provided email address belongs to an existing user. This function is a core part of WordPress; if a user with that email address is found, it retrieves the WP_User object.
