WooCommerce仅显示已购买的商品


WooCommerce Display Purchased Items Only

所以我在网上做了很多查找,但找不到解决方案。。。

基本上,我想做的是显示用户在商店购买的所有产品的产品循环,就像显示普通产品一样。

如果你仍然不明白,也许这会帮助你理解我的意思。。

以下是WooCommerce文档中的示例产品循环。。。

<ul class="products">
    <?php
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 12
            );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();
                woocommerce_get_template_part( 'content', 'product' );
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
    ?>
</ul><!--/.products-->

所以,如果我想显示基本上相同的产品循环,但将其过滤掉,使其只显示用户已经购买的产品,该怎么办。

老实说,我不知道该怎么办,我相信过去也有其他人对此进行过研究,所以也许这会帮助很多人!

提前感谢!

至少有两种不同的方法可以用来解决这个问题。

第一种方法是从每个帖子中获取产品,然后从每个产品中获取产品ID,然后使用if语句使用wc_customer_bought_product或woocommerce_customer_bought_production进行筛选(如果您使用的是旧的WooCommerece)。

第二种方法是传递正确的参数来过滤WP_Query,使其只包括用户购买的订单,然后只过滤这些订单中的产品。有关第二种方法的更多信息,请访问"获取所有用户订单和用户在WooCommerce商店购买的产品"(archive.org)

第一种方法的一个例子是类似的东西

<!-- code started -->
<ul class="products">
    <?php
        $user_id = get_current_user_id();
        $current_user= wp_get_current_user();
        $customer_email = $current_user->email;
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 12
            );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post(); $_product = get_product( $loop->post->ID );
            if (wc_customer_bought_product($customer_email, $user_id,$_product->id)){
                woocommerce_get_template_part( 'content', 'product' );
            }
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
    ?>
</ul><!--/.products-->

感谢Appleman1234提供了两个答案,这两个答案都会起作用。

ApppleMan1234提供了一个例子,他给出的第一个答案是遍历所有产品,然后通过调用wc_customer_bought_product()来过滤它们。这肯定会奏效。如果您有n产品,那么您将进行n+1数据库查询。

他的第二个建议是链接Brajesh Singh撰写的一篇帖子,他于2013年6月2日在fusedpress.com上发布了一个解决方案。原始帖子已不可用。我在谷歌上找到了一份缓存副本。

Brajesh Singh的解决方案查询用户的订单,然后查询订单详细信息,最后查询订单项元数据中的产品id。这个解决方案总是只有3个查询。除非你的商店只有一两种产品,否则这种解决方案要好得多。

这是Brajesh Singh代码的一个经过轻微编辑的版本。

/**
 * Get all Products Successfully Ordered by the user
 * @return bool|array false if no products otherwise array of product ids
 */
function so28362162_get_all_products_ordered_by_user() {
    $orders = so28362162_get_all_user_orders(get_current_user_id(), 'completed');
    if(empty($orders)) {
        return false;
    }
    $order_list = '(' . join(',', $orders) . ')';//let us make a list for query
    //so, we have all the orders made by this user that were completed.
    //we need to find the products in these orders and make sure they are downloadable.
    global $wpdb;
    $query_select_order_items = "SELECT order_item_id as id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id IN {$order_list}";
    $query_select_product_ids = "SELECT meta_value as product_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key=%s AND order_item_id IN ($query_select_order_items)";
    $products = $wpdb->get_col($wpdb->prepare($query_select_product_ids, '_product_id'));
    return $products;
}
/**
 * Returns all the orders made by the user
 * @param int $user_id
 * @param string $status (completed|processing|canceled|on-hold etc)
 * @return array of order ids
 */
function so28362162_get_all_user_orders($user_id, $status = 'completed') {
    if(!$user_id) {
        return false;
    }
    $args = array(
        'numberposts' => -1,
        'meta_key' => '_customer_user',
        'meta_value' => $user_id,
        'post_type' => 'shop_order',
        'post_status' => 'publish',
        'tax_query' => array(
            array(
                'taxonomy' => 'shop_order_status',
                'field' => 'slug',
                'terms' => $status
            )
        )
    );
    $posts = get_posts($args);
    //get the post ids as order ids
    return wp_list_pluck($posts, 'ID');
}

将其与问题中的乘积循环相结合,再加上一个不推荐使用的wc_get_template_part()和一个添加的posts_per_page=-1,就得到了

<ul class="products">
        <?php
        $args = array(
            'post_type' => 'product',
            'post__in' => so28362162_get_all_products_ordered_by_user(),
            'posts_per_page' => -1
        );
        $loop = new WP_Query($args);
        if($loop->have_posts()) {
            while($loop->have_posts()) : $loop->the_post();
                wc_get_template_part('content', 'product');
            endwhile;
        }
        else {
            echo __('No products found');
        }
        wp_reset_postdata();
        ?>
    </ul><!--/.products-->

不确定这是否对你有帮助,但WooThemes开发了一个插件来支持购买历史。