获取所有在Woocommerce购买商品的客户的列表

时间:2018-09-12 00:13:17

标签: php wordpress woocommerce orders customer

如何获得至少购买了某件商品的客户列表,以及是否有可能按订单数量订购?据我了解,_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; ?>

1 个答案:

答案 0 :(得分:3)

尝试以下使用非常简单的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");
print_pr($customer_ids);
$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; ?>

经过测试可以正常工作。

  

用户 meta_key _order_count _order_total 不存在,直到{{1} } get_order_count()get_total_spent()方法为每个客户运行一次。当为客户执行这些方法时,相关的元值也会更新。

     

函数wc_get_customer_total_spent()wc_get_customer_order_count()执行这些方法并刷新相应的用户元数据。