50
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!
PHP Function: Get List of All WooCommerce “Customers”
/**
* @snippet Get Array Of WooCommerce Customers
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 6
* @donate $9 https://tutoraspire.com
*/
function tutoraspire_customer_list() {
$customer_query = new WP_User_Query(
array(
'fields' => 'ID',
'role' => 'customer',
)
);
return $customer_query->get_results();
}
Usage: call the tutoraspire_customer_list() function and then loop over the returned array to go through each customer and get whatever you need:
foreach ( tutoraspire_customer_list() as $customer_id ) {
$customer = new WC_Customer( $customer_id );
echo $customer->get_billing_first_name() . ' ' . $customer->get_billing_last_name();
}