WooCommerce仅显示购买的商品

时间:2015-02-06 09:11:00

标签: php wordpress woocommerce

所以我做了很多环顾网络,无法找到解决方案......

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

如果你仍然不明白这可能会帮助你明白我的意思..

以下是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-->

那么,如果我想基本上显示相同的产品循环,但过滤掉它,以便它只显示用户已购买的产品。

老实说,我不知道这个去哪里,我相信还有其他人在过去做过这方面的研究,所以也许这会帮助一群人!

提前致谢!

2 个答案:

答案 0 :(得分:9)

您可以采取至少两种不同的方法来解决此问题。

首先是从每个帖子中获取产品,然后从每个产品中获取产品ID,然后使用if语句过滤使用wc_customer_bought_product或woocommerce_customer_bought_product(如果您使用的是旧的WooCommerece)。

第二种方法是传递正确的参数来过滤WP_Query,使其仅包含用户购买的订单,然后仅按这些订单过滤产品。有关第二种方法的更多信息,请访问Get All User Orders and Products bought by user in WooCommerce based shop (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-->

答案 1 :(得分:-2)

不确定这是否会对您有所帮助,但WooThemes开发了plugin来支持购买历史记录。

相关问题