WooCommerce 仅显示购买的物品 [英] WooCommerce Display Purchased Items Only

查看:21
本文介绍了WooCommerce 仅显示购买的物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在网上找了很多遍,但找不到解决方案......

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

如果你还是不明白,也许这会帮助你理解我的意思..

这是 WooCommerce 文档中的示例产品循环...

    <?php$args = 数组('post_type' =>'产品','posts_per_page' =>12);$loop = new WP_Query( $args );如果 ( $loop->have_posts() ) {while ( $loop->have_posts() ) : $loop->the_post();woocommerce_get_template_part('内容','产品');终了;} 别的 {echo __('没有找到产品');}wp_reset_postdata();?></ul><!--/.products-->

那么,如果我想显示基本上完全相同的产品循环,但将其过滤掉,使其仅显示用户已经购买的产品,该怎么办.

老实说,我不知道用这个去哪里,我相信过去还有其他人对此进行过研究,所以也许这会对很多人有所帮助!

提前致谢!

解决方案

您至少可以采用两种不同的方法来解决这个问题.

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

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

第一种方法的示例类似于

<ul class="产品"><?php$user_id = get_current_user_id();$current_user= wp_get_current_user();$customer_email = $current_user->email;$args = 数组('post_type' =>'产品','posts_per_page' =>12);$loop = new WP_Query( $args );如果 ( $loop->have_posts() ) {while ( $loop->have_posts() ) : $loop->the_post();$_product = get_product( $loop->post->ID);如果 (wc_customer_bought_product($customer_email, $user_id,$_product->id)){woocommerce_get_template_part('内容','产品');}终了;} 别的 {echo __('没有找到产品');}wp_reset_postdata();?></ul><!--/.products-->

So I have done a bunch of looking around the web and couldn't find a solution for this...

Basically what I am trying to do is display a product loop of all the products the user has purchased in the store just like displaying normal products.

If you still don't understand maybe this will help you get what I mean..

Here is the example product loop on the WooCommerce documentation...

<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-->

So what if I wanted to display basically this same exact product loop however filter it out so that it only displays products that the user has already purchased.

I honestly do not know where to go with this one and I am sure there are others that have done research on this in the past so maybe this will help out a bunch of people!

Thanks in advance!

解决方案

There are at least two different approaches you can take to solve this problem.

The first is to get the product from each post, and then get the product ID from each product and then use an if statement to filter using wc_customer_bought_product or woocommerce_customer_bought_product (if you are using old WooCommerece).

The second is to pass the correct arguments to filter the WP_Query to only include orders purchased by a user and then filter products only in those orders. More information on the second approach is available at Get All User Orders and Products bought by user in WooCommerce based shop (archive.org).

An example of the first approach is something like

<!-- 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-->

这篇关于WooCommerce 仅显示购买的物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆