Skip to content

Instantly share code, notes, and snippets.

@DevWael
Created July 9, 2019 10:12
Show Gist options
  • Save DevWael/c6e2ed3d28e2954477069ae36c588be1 to your computer and use it in GitHub Desktop.
Save DevWael/c6e2ed3d28e2954477069ae36c588be1 to your computer and use it in GitHub Desktop.
List top woocommerce customers by their total spent
<?php
function prefix_top_customers_with_total_spend( $length = 5 ) {
if ( ! class_exists( 'woocommerce' ) ) {
return;
}
$data = [];
$args = [
'role' => 'customer',
'number' => - 1,
'fields' => 'ID'
];
$query = new WP_User_Query( $args );
$customers = $query->get_results();
foreach ( $customers as $customer_id ) {
$data[ $customer_id ] = wc_get_customer_total_spent( $customer_id );
}
arsort( $data );
$data = array_slice( $data, 0, $length );
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment