In WooCommerce, we can easily get the customers/users who have already purchased items from our store. If someone wants to remove accounts from the database who haven’t made a purchase, then we can use the below code:
$all_users = get_users();
if ( !empty( $all_users ) ) {
$no_order_user_list = [];
foreach($all_users as $user) {
if ( in_array( 'administrator', $user->roles ) )
continue;
$customer_orders = get_posts(array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user->ID,
'post_type' => wc_get_order_types(),
'post_status' => array('wc-pending', 'wc-processing', 'wc-completed') //array_keys(wc_get_order_statuses()),
));
if( count( $customer_orders ) == 0 ) {
$no_order_user_list [] = $user;
}
}
}
echo '<pre>';
print_r($no_order_user_list);
echo '</pre>';