【问题标题】:WC Get Orders with Sort by Highest Total Number of OrdersWC 获取按最高订单总数排序的订单
【发布时间】:2019-10-31 02:48:20
【问题描述】:

我正在尝试通过 WC get orders 查询最高客户的订单总数,但似乎无法通过 wc_get_orders 获取该信息。希望有人可以指导我。这是我在 sn-p 中的尝试,但它只返回每个客户的销售总数,而不是从最高到最低排序。

$i = 0;

    foreach ($customer_ids as $customer_id) :
        $customer = new WP_User($customer_id);


        $args = array(
          'status' => 'completed',
          'customer_id' => $customer_id,
          'limit' => '-1',
        );

        $orders      = wc_get_orders( $args );

        update_option( '_gpdebug_contributor_list_d',$orders );

        ?>
        <?php
        $i++;
        if (!empty($orders)) : ?>
        <tr>
            <td><?php echo $i; ?></td>
            <td><?php $bp_name = bp_core_get_userlink( $customer_id ); ?><?php echo get_avatar($customer_id, 30) . $bp_name;
            if ( $i == 1 ) {
              echo '<span class="ion-trophy gold"></span>';
            } elseif ( $i == 2 ) {
              echo '<span class="ion-trophy silver"></span>';
            } elseif ( $i == 3 ) {
              echo '<span class="ion-trophy bronze"></span>';
            }
            ?></td>
            <td style="text-align: right;">
              <?php

                $total = 0;

                foreach ( $orders as $order ) {
                  $customer_order = wc_get_order( $order );
                  $total += $customer_order->get_total();
                }
                echo $total;

提前致谢。

【问题讨论】:

    标签: woocommerce


    【解决方案1】:

    我了解到您已经获得了每位客户的订单总数。这将需要对您的代码进行一些更改,但我认为您可以轻松修改它。

    创建一个数组,

    $customer_totals = array();
    

    然后插入每个客户 ID 作为键,并将该客户的订单总数作为值 (*),

    $temp_arr = array($customer_id => $total);
    $customer_totals =array_merge($customer_totals , $temp_arr);
    

    插入所有客户ID和总数后,按降序(**)对最终数组进行排序,

    arsort($customer_totals);
    

    现在您可以根据需要显示总数。我希望这有帮助。如果您有任何问题,请通过更新代码通知我。

    *https://stackoverflow.com/a/9735696

    **https://www.w3schools.com/php/func_array_arsort.asp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 2023-01-21
      • 1970-01-01
      • 2017-10-27
      • 2018-03-16
      • 2022-11-03
      相关资源
      最近更新 更多