Display only WooCommerce

So, I did a bunch of network searches and couldn't find a solution for this ...

Basically, what I'm trying to do is display the product cycle of all the products that the user purchased in the store, for example, to display regular products.

If you still do not understand, perhaps this will help you understand what I mean.

Here is an example product cycle in 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 exact exact product cycle, filter it so that it displays only those products that the user has already purchased.

I honestly don’t know where to go with this, and I’m sure there are others who have done research on this in the past, so maybe this can help get people together!

Thanks in advance!

+6
source share
3 answers

To solve this problem, you can take at least two different approaches.

First, to get the product from each message, and then get the product identifier from each product, and then use the if statement to filter using wc_customer_bought_product or woocommerce_customer_bought_product (if you are using the old WooCommerece).

The second is to pass the correct arguments to filter WP_Query to include only orders purchased by the user, and then filter products only in those orders. More detailed information on the second approach can be found at Get all user orders and products purchased by a user in a store based on WooCommerce (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--> 
+9
source

Kudos to Appleman1234 for two answers, both of which will work.

ApppleMan1234 the first answer he gave as an example is to look at all the products, and then filter them by calling wc_customer_bought_product() . This will certainly work. If you have n products, you will make n+1 queries to the database.

His second sentence is a link to a post written by Brages Singh, who posted the decision on fusedpress.com on June 2, 2013. The original post is no longer available. I found a cached copy on Google.

The Brajesh Singh solution requests custom orders, then requests the details of the order and the latter requests the product identifier in the order item metadata. This solution will always be only 3 queries. If your store has only 1 or 2 products, this solution is much better.

Here is a slightly edited version of Brajesh Singh's code.

 /** * 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'); } 

Combining this with the product loop from the question, plus the not obsolete wc_get_template_part() and adding posts_per_page=-1 gives us

 <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--> 
+8
source

Not sure if this will help you at all, but there is a plugin developed by WooThemes to support your purchase history.

-2
source

Source: https://habr.com/ru/post/982086/


All Articles