WooCommerce – Get users list with their total order price in the last 30 days
Question
I have users list in table with this code :
$args = array(
'number' => $users_per_page,
'paged' => $current_page,
'order' => 'ASC',
'orderby' => 'id',
'count_total' => true,
);
$get_all_users_mdp = new WP_User_Query($args);
if($get_all_users_mdp->get_results()){
foreach($get_all_users_mdp->get_results() as $users){
$users_total_order[] = ['username' => $users->user_login , 'email' => $users->user_email , 'role' => translate_user_role($wp_roles->roles[$users->roles[0]]['name']) , 'totalOrder' => number_format(get_user_orders_total($users->id) , 0, ',', ',')];
}
}
function get_user_orders_total($user_id) {
// Use other args to filter more
$args = array(
'customer_id' => $user_id
);
// call WC API
$orders = wc_get_orders($args);
if (empty($orders) || !is_array($orders)) {
return 0;
}
// One implementation of how to sum up all the totals
$total = array_reduce($orders, function ($carry, $order) {
$carry += (float)$order->get_total();
return $carry;
}, 0.0);
return $total;
}
and i want to get all users with their total order price within a month and pagination, the problem is , I’m getting result correctly without pagination but when pagination comes up, i don’t have a correct result, I just want to sort users with highest order price (ASC) . i don’t know what to do now.
0
4 months
0 Answers
14 views
0
Leave an answer
You must login or register to add a new answer .