Get a list of all customers in WooCommerce with PHP

/**
 * Prints WooCommerce customer list (with order count).
 *
 * @see https://wpcodebook.com/woocommerce-list-customers-php/
 * @see https://developer.wordpress.org/reference/functions/get_users/
 * @see https://developer.wordpress.org/reference/classes/wp_user_query/
 * @see https://woocommerce.github.io/code-reference/classes/WC-Customer.html
 */
function wpcodebook_print_customers() {

	$users = get_users( array( 
		'role__in' => array( 'customer' ),
		'orderby'  => 'display_name',
		'order'    => 'ASC',
	) );

	foreach ( $users as $user ) {
		if ( ( $customer = new WC_Customer( $user->ID ) ) ) {
			printf( '%s (#%d) - %d<br>',
				$customer->get_display_name(),
				$customer->get_id(),
				$customer->get_order_count()
			);
		}
	}

}

Example output:

James Doe (#18) - 5
Jane Doe (#161) - 8
John Doe (#154) - 15
Judy Doe (#198) - 0

Leave a Comment