Created
July 9, 2019 10:12
-
-
Save DevWael/c6e2ed3d28e2954477069ae36c588be1 to your computer and use it in GitHub Desktop.
List top woocommerce customers by their total spent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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