Get WooCommerce Customer by Billing Email

It’s possible to get the data of a WooCommerce customer when all you have is their billing email address. I’m going to assume this is a more advanced function and you already know how you want to use it and where you would put it. If I assumed incorrectly, let me know in the comments.

The function is quite simple really:

  1. Firstly, we give ourselves access to the WordPress database with the global $wpdb line.
  2. Next, we use the get_var method of $wpdb to return a single result; the contents of the user_id column if our conditions are met.
  3. We are searching in the $wpdb->usermeta table. The meta_key must equal billing_email and the meta_value must equal the email address we pass into the function.
  4. If an ID is not found, we return false.
  5. If an ID is found, we continue on and generate a new WC_Customer(). This is a WooCommerce class which will create a customer object.
  6. If the customer object was not created successfully, we return false.
  7. Otherwise, return the $customer object.

So now you can use the function wherever you like:

It will either return false if no customer is found, or a WC_Customer object.

Once you have a WC_Customer object, you can access a bunch of methods, like so:

Note: These methods are mostly available in WooCommerce 3.0.0 and above. The function above will still return a customer object in lower versions of WooCommerce, but you access the data slightly differently.

Leave a Reply

2 Comments on "Get WooCommerce Customer by Billing Email"

avatar
  Subscribe  
newest oldest most voted
Notify of
rollz
Guest

maybe better to use WP native method to query users table:
// Check against billing emails of existing users.
$users_query = new WP_User_Query( array(
‘fields’ => ‘ID’,
‘meta_query’ => array(
‘key’ => ‘_billing_email’,
‘value’ => $check_emails,
‘compare’ => ‘IN’,
),
) );