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
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.
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
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.
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
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:
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?