Skip Navigation

[Resolved] Show product content only if purchased

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

Problem:
How can I show a Product's content only if the User purchased the Product, with Toolset and WooCommerce?

Solution:
This requires a Custom Code.
The process is outlined here:
https://toolset.com/forums/topic/show-full-video-to-paid-users-and-trailer-to-others/#post-424693

100% of people find this useful.

This support ticket is created 7 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 8 replies, has 2 voices.

Last updated by Monetizer 7 years, 8 months ago.

Assisted by: Beda.

Author
Posts
#424269

Here is what I am trying to do:
1. I have two separate videos; one is a trailer, and other is a full-length movie. I want the users who I have not yet purchased the WooCommerce product to see the trailer and the person who has bought should be able to see the full video.

2. I was following the guide here: https://toolset.com/forums/topic/how-to-show-limited-product-media-access-before-purchase/
But I have some issues:
(1) Do I have to assign this video on products?
(2) If I have to assign this view to the page and would like to pass the product id to check if the user has purchased the product or not e.g.,
[woo-purchased-product id=5]

How can I pass this product id in views?

Look forward to hearing from you.

Thanks

#424693

Since Minesh 3 currently has a few days off, I will replace him here in the forum.
I hope this is OK for you.

1. This should be possible with the WooCoomerce API and a HTML conditional.

An example here:
https://toolset.com/forums/topic/how-to-show-limited-product-media-access-before-purchase/#post-341791

In Short:

This shortcode evaluates if a certain (logged in) user has bought a certain (current in loop) product.

The WooCommerce function seems buggy and some attention is needed.
Check the code first to see if it is still working with the workaround.

- functions.php:

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()));
}

Register this shortcode in Views > Settings:

wpv-product-purchased

Then evaluated as this in a View as example:

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

The function of WooCommerce itself is returning a error, and the ShortCode works as expected, although it returns a numerical value “1” for bought and a empty value '' for not bought, not a boolean “true” or “false” as stated by WooCommerce.

This might be a Bug in their code or misunderstanding on my end reading the Core code of WooCommerce, and it could therefore change in future.

2. Should be obsolete with above solution, correct?

#424695

Hi Beda,

Thank you for getting back to me.

Does it mean I have to use Views for Woocommerce and design the layout of product pages and use this conditional view on those pages?

For the second part, I wanted to use this view on regular pages (not product pages) and make sure the product is purchased via sending the product id as attributes, is this somehow possible or any other similar example to pass attributes to shortcode via views?

Look forward to hearing from you.

Thanks

#424699

You can do it anywhere.
The Shortcode will grab the ID of the Product (current in loop) and user (current logged in user).

That means you can createa View with Products in the Loop and apply the ShortCode.

No WooCommerce Views is necessary for this.

You can modify the ShortCode to your needs, but that is above our support goals.
The above is a working example that shows how you can achieve this.

This is the exact workflow that Eric in the other Thread followed.

#424725

Hi Beda,

It doesn't make sense to me, how would it get product loop on a page, I am sure I am missing something.

I have coded the plugin that is working fine when I place a shortcode on the page [woo-purchased-product id=5], it would show if id five has been purchased by current logged in user or not.

How can I use this shortcode on views (i want to pass an id to this shortcode)?

Thanks

#424736

1. How would it get product loop on a page, I am sure I am missing something.

The ShortCode is used on a Single Product.

2. To pass a ID, you can add a attribute to your function(ShortCode)
As example with $atts['id']

The ShortCode would look like this:

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, $atts['id']));
}

Then you should be able to pass it to the ShortCode as this:
[wpv-product-purchased id='2']

I am not sure this will work in a conditional, as WordPress is a bit tricky with nested shortcodes.
Please let me know if this works.

Please aknowledge that this is Custom Code using a 3rd Party API.
We can be of limited assistance only.
https://toolset.com/toolset-support-policy/

Thank you for understanding

#424742

Beda, I am not sure if you are even reading what i am saying....

I have the plugin coded already and it is working fine with shortcode on a page with a product id...

How can i use this shortcode on VIEWS and pass product id to it with conditional?

[wpv-conditional if="( '[wpv-product-purchased id='2']' eq '1' )"]test[/wpv-conditional]

[wpv-conditional if="( '[wpv-product-purchased id='2']' eq '' )"]test[/wpv-conditional]

I hope i am clear this time?

#424750

I am reading your replies carefully.

In Toolset, you can use any ShortCode that your Theme or a Plugin adds.

I am not sure what Plugin you are coding, but yes, the equation you use in your HTML conditional is exactlty what I suggest to use.
Please read my reply above.

As I said, I am not sure this will work properly in a Conditional as WordPress is restrictive about Nested ShortCodes.

If you want to pass the ID to that ShortCode dinymically, you can substitue the ID attribute with id="[wpv-post-id]" which if placed in a View or Post (product) would return the ID of the current Post.

Again, it's once more a nested ShortCode and I can not promise it will work.

This is the way you will pass an ID to your ShortCode in case you do not want to hardcode it.
You will use the [wpv-post-id] ShortCode provided in our GUI and documented here:
https://toolset.com/documentation/views-shortcodes/#wpv-post-id

As I elaborated, you will need to create a View querying Products, if you want to check against several Porducts on the same page.

#424782

Thank you the shortcode worked with arguments with conditionals.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.