Skip Navigation

[Resolved] Check if a user has bought a product

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can I check if a certain user already bought a certain Product in WooCommerce?

Solution:
This depends entirely on WooCommerce.
We do not control the shopping process of the named plugin.
We just allow to style it or append products to CRED Forms.

It is hence not within the goal of Toolset to provide such functionality.

However, you can use a Custom Solution:
https://toolset.com/forums/topic/sites-users-personal-product-list/#post-607289

This support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 6 replies, has 2 voices.

Last updated by seppeL 6 years, 10 months ago.

Assisted by: Beda.

Author
Posts
#605804

Hi,
I'm making an ecommerce website, selling wine for professional businesses. Only registered users can see and order my products.
I would like for every user(client) of my site to have their own page with products they order on a weekly basis. This to make ordering faster and easier.

I've been trying with many-to-many relationships
https://toolset.com/documentation/beyond-the-basics/post-relationship-concepts/implementing-many-to-many-relationships/
and different query filters.

I can't figure out how to implement this on my site.

Any help would be much appreciated.

Thanx,
Sep

#605964

I assume you use and sell products set up with WooCommerce.

In either case, what you need is a flag that tells the code "Hey, this user bought this product".

As far I recall, WooCommerce offers a function that does exactly that, it returns true if a product is bought by an user.
https://docs.woocommerce.com/wc-apidocs/function-wc_customer_bought_product.html

So, you can use that function to check upon a product/user.

One way to do that can be to register the function in Toolset > Settings, and use it as a HTML conditional source.
That would hide all products in a View that are not bought by the current user.

It might be too heavy, if you have many products.

In this case I rather suggest to filter your View with our API code:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

You can fire the WooCommerce function within that hook and limit the results to products that where bought.

#606296

Thanks for your response Beda,

I think I'm going at this all wrong, when adding the API code to the filter, no products are showing on the output.
Where and how would you add the filter?

Thanks in advance,
Sep

#606707

To the active Theme's functions.php

Please show me your filter, in case this still does not work.

I can then check if there are some errors.

#607233

Thanks Beda,

I'm still new to the php-part. Learning quick on some parts, but I still can't figure out how to write the new filter.
I tried:

add_filter( 'wpv_filter_query', 'wc_customer_bought_product' );

or

add_filter( 'wpv_filter_query', 'my_product_list');
function my_product_list ( ) {
 global $woocommerce;
$user_id = get_current_user_id();
$current_user= wp_get_current_user();
$customer_email = $current_user->email;

foreach($product_ids as $item):
    if ( wc_customer_bought_product( $customer_email, $user_id, $item) )
       return true;
    endforeach; 

return false;
}

I've tried another solution of yours
https://toolset.com/forums/topic/how-to-show-limited-product-media-access-before-purchase/
But this also isn't working.

Sep

#607289

I was wrong.

The WooCommerce function "wc_customer_bought_product()" expects at least Product ID and one of both either User ID or User email.
Otherwise it won't return the right results.

In the Views filter "wpv_filter_query()" we have no Post Data yet, as it is before we get results, before the Query is even passed.

So this function cannot be used at this moment.
It can only be used once you have the Post Object ready to use.

This is letting us HTML conditional as elaborated in that other ticket.

You can use the Function directly in the condition, or you can do what I suggested on the other thread, which still works fine for me:

1. Register the ShortCode:

add_shortcode('wpv-product-purchased', 'wpv_product_purchased_func');
   
function wpv_product_purchased_func($atts){
  $current_user = wp_get_current_user();
  $email = $current_user->email;
  return (wc_customer_bought_product( $email, $current_user->ID, get_the_ID()));
}

2. Register the ShortCode in Toolset > Settings > Front End Content > Third-party shortcode arguments:

wpv-product-purchased

3. Use it in a View's Loop where you query Products:

[wpv-conditional if="( '[wpv-product-purchased]' eq '1' )"]
  [wpv-post-title]
[/wpv-conditional]

I tested this just now, it works.

Please can you re-test them?

#609622

Thanks Beda,

This did the trick!

Cheers!

The forum ‘Types Community Support’ is closed to new topics and replies.