WooCommerce显示已购买的商品只有

WooCommerce显示已购买的商品只有

问题描述:

所以我做了一堆看各地的网络,但没有找到一个解决方案...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--> 

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

我真的不知道该去哪里,我肯定还有其他人在过去做过这方面的研究,所以这可能会帮助一群人!

提前致谢!

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

首先是从每个帖子获取产品,然后从每个产品获取产品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--> 
+0

如果我只希望在某个类别中展示产品,该怎么办? – kevingilbert100 2015-02-10 04:57:45

+0

好吧我自己想出了类别,但是另一个问题是如果我想让它检查元变量。例如,如果用户有持续的订阅,这个正在进行的订阅将在每个产品元中在“payment_plan_subscription_id_text_field”下定义,并且插件im使用具有以下函数“woocommerce_members_only(array())”的检查,那么如果我想说如果客户已购买该产品或||如果客户在产品元内的该会员组内。 – kevingilbert100 2015-02-10 05:42:47

+0

@ kmgilbert100你可以使用给定链接中的代码来实现该目的..请阅读;) – Reigel 2015-02-12 03:37:21

不知道这是否可以帮助你,但有一个由WooThemes开发的plugin支持购买历史记录。

+0

我认为楼主是要求一个页面展现给用户所购买的所有产品,而WooCommerce客户历史记录插件为店主提供后端分析 – Kirby 2015-04-29 18:59:33

荣誉给Appleman1234提供两个答案,两者都将正常工作。

ApppleMan1234的第一个答案,他提供了一个例子,是循环遍历所有产品,然后通过调用wc_customer_bought_product()来过滤它们。这当然会起作用。如果您有n产品,那么您将制作n+1数据库查询。

他的第二个建议是链接到Brajesh Singh撰写的帖子,他在2013年6月2日发布了fusedpress.com上的解决方案。原始帖子不再可用。我在Google上找到cached copy

Brajesh Singh的解决方案查询用户的订单,然后查询订单详细信息,并在订单商品的元数据中查询产品ID。这个解决方案总是只有3个查询。除非你的店铺只有1或2种产品,否则这种解决方案要好得多。

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

恐怕您的缓存链接也已关闭:(谢谢您的解释。我会尝试AppleMan1234的第二个解决方案(非常感谢你)。 – 2016-05-31 08:49:50