Menu Close

WooCommerce: Get List Of All Customers

default

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 forums or read thoroughly the WooCommerce core files on a daily basis – so it must’ve been one of the two. Enjoy!

This is the backend area for WordPress users = “customers”. You will be able to get this exact list with the code below in case you need it elsewhere e.g. a dropdown for the admin, a custom report for store managers, and so on.

PHP Function: Get List of All WooCommerce “Customers”

/**
 * @snippet       Get Array Of WooCommerce Customers
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

function bbloomer_customer_list() {
	$customer_query = new WP_User_Query(
		array(
			'fields' => 'ID',
			'role' => 'customer',			
		)
	);
	return $customer_query->get_results();
}

Usage: call the bbloomer_customer_list() function and then loop over the returned array to go through each customer and get whatever you need:

foreach ( bbloomer_customer_list() as $customer_id ) {
	$customer = new WC_Customer( $customer_id );
	echo $customer->get_billing_first_name() . ' ' . $customer->get_billing_last_name();
}
View Source
Posted in WooCommerce Tips