php - 获取已购买商品的所有客户的列表

标签 php wordpress woocommerce orders

我如何获得至少购买了某样东西的客户列表,以及是否可能按订单数量对他们进行排序?根据我的理解,_order_count 元键存在,并且如果他们下了订单,则设置为整数订单数。

此查询忽略订单计数,只显示所有用户。

$args = array(
     'role' => 'customer',
     'number' => -1,
     'order' => 'ASC',
     'orderby' => 'meta_value',
     'meta_query' => array(
          array(
               'key'     => '_order_count',
               'value'   => array(''),
               'compare' => 'NOT IN'
          )
    )
);

$query = new WP_User_Query($args);

这是一个表,它将回显有关用户花费了多少以及他下了多少订单的信息

<?php if (! empty($query->get_results())) : ?>

     <table class="table">

          <thead>
                    <tr>
                         <th data-sort="string">Name</th>
                         <th data-sort="string">Email</th>
                         <th data-sort="float">Total spent (<?php echo get_option('woocommerce_currency'); ?>)</th>
                         <th data-sort="int">Order placed</th>
                    </tr>
          </thead>

          <tbody>

          <?php foreach ($query->get_results() as $user) : ?>

               <?php $customer = new WP_User($user->ID); ?>

               <tr>
                    <td><?php echo $user->display_name; ?></td>
                    <td><?php echo $user->user_email; ?></td>
                    <td><?php echo wc_get_customer_total_spent($user->ID); ?></td>
                    <td>
                    <?php 
                    $customer_orders = get_posts(array(
                         'numberposts' => -1,
                         'meta_key'    => '_customer_user',
                         'meta_value'  => $user->ID,
                         'post_type'   => wc_get_order_types(),
                         'post_status' => array('wc-pending', 'wc-processing', 'wc-completed') //array_keys(wc_get_order_statuses()),
                    ));
                    echo count($customer_orders);
                    ?>
                    </td>
               </tr>

          <?php endforeach; ?>

          </tbody>
     </table>

<?php endif; ?>

最佳答案

尝试使用以下代码,该代码使用非常简单的 SQL 查询来获取 $customers ID。对于客户订单计数,您可以使用专用函数 wc_get_customer_order_count()

<?php
global $wpdb;
$customer_ids = $wpdb->get_col("SELECT DISTINCT meta_value  FROM $wpdb->postmeta
    WHERE meta_key = '_customer_user' AND meta_value > 0");
$currency = ' (' . get_option('woocommerce_currency') . ')';
if (sizeof($customer_ids) > 0) : ?>
<table class="table">
    <thead>
        <tr>
            <th data-sort="string"><?php _e("Name", "woocommerce"); ?></th>
            <th data-sort="string"><?php _e("Email", "woocommerce"); ?></th>
            <th data-sort="float"><?php _e("Total spent", "woocommerce"); echo $currency; ?></th>
            <th data-sort="int"><?php _e("Orders placed", "woocommerce"); ?></th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($customer_ids as $customer_id) :
        $customer = new WP_User($customer_id); ?>
        <tr>
            <td><?php echo $customer->display_name; ?></td>
            <td><?php echo $customer->user_email; ?></td>
            <td><?php echo wc_get_customer_total_spent($customer_id); ?></td>
            <td><?php echo wc_get_customer_order_count($customer_id); ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
<?php endif; ?>

经过测试并有效。

Users meta_key _order_count and _order_total doesn't exist until WC_Customer_Data_Store get_order_count() and get_total_spent() methods have run once for each customer. Also the related meta values are also updated when those methods are executed for a customer.

The functions wc_get_customer_total_spent() and wc_get_customer_order_count() execute those methods and refresh the corresponding user meta data.

关于php - 获取已购买商品的所有客户的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52285730/

相关文章:

php - 回调函数在所有验证之前调用

php - 如何为 DOMDocument XML 的每个元素添加引号以保存在 MySQL 数据库中

css - 改变文字颜色

php - 反序列化mysql表中的数据并通过php输出?

php - 在 Woocommerce 中使用 ajax 更新购物车小部件中的详细信息,无需加载页面

php 7 --with-config-file-scan-dir 不工作

用于下载文件的 PHP 脚本在 IE 中不起作用

jquery - 如何在 jQuery 中只显示菜单的子菜单?

wordpress - 让 Woocommerce 默认管理库存

php - WooCommerce 条件自定义字段(如果购物车中的产品类别)