Hello,
Below is an example function that will tell you if the current user has bought certain product:
function user_has_bought($pid){
$db = Factory::getDBO();
$db->setQuery("SELECT count(p.order_item_id) FROM wp_postmeta as o
INNER JOIN wp_shop_order_item as p ON p.order_id = o.post_id
WHERE o.meta_key = '_customer_id' AND o.meta_value = '". (int)get_current_user_id()."' AND p.product_id = ".$pid);
return (int)$db->loadResult();
}
You need to pass the id of the product as argument.
Best Regards
Hello orillacart …
I’m really sorry, but I’m new at this. Where should this function be placed ? How do I implement it ?
Thanks !
Best regards.
Hello,
This is an example function to return if the current user has bought certain product. Unfortunately to achieve what you want, you will need help of experienced PHP/WP developer. Perhaps in your case a plugin like s2member will be more suitable?
https://wordpress.org/plugins/s2member/
Best Regards
I am okay with PHP, as in I can implement stuff. I just want to know where it should be placed and how it should be accessed. Please help, orillacart !
Well in that case you should create new plugin that rely on that function
and restricts the viewed page based on the result returned.
Another way would be to add that function in the template functions.php and do the check in the page template. Probably that would be simpler.
Best Regards
I have another, possibly last doubt Orillacart … sorry !
How to access the function in page.php ? And
What does the function return ? Exactly ?
function user_has_bought($pid){
$db = Factory::getDBO();
$db->setQuery(“SELECT count(p.order_item_id) FROM wp_postmeta as o
INNER JOIN wp_shop_order_item as p ON p.order_id = o.post_id
WHERE o.meta_key = ‘_customer_id’ AND o.meta_value = ‘”. (int)get_current_user_id().”‘ AND p.product_id = “.$pid);
return (int)$db->loadResult();
}
And product ID, as in SKU ?
Thanks again !
Best regards.
No, the product id would be the id of the post in the database.
Best Regards
Orillacart, last question. Id of the post in database is Permalink of post of the product or actual mySQL id ??
Please bear with me !
Thanks again.
Best Regards.
The actual mySQL numeric ID of the post(product).
Best Regards