WooCommerce-获取用户列表及其最近30天内的总订单价格

时间:2020-09-24 08:08:40

标签: wordpress woocommerce

我在用户列表中包含以下代码:

            $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;
            }

我想在一个月内获得所有用户的总订单价格并进行分页,问题是,我没有分页就可以正确获得结果,但是当出现分页时,我没有正确的结果,我只是想要对具有最高订购价格(ASC)的用户进行排序。我不知道现在该怎么办。

0 个答案:

没有答案