How To Check If User Purchased An Item On The Product Page

how can I check if a buyer purchased an item, I'm trying to show verified purchase on reviews that where left by users who purchased the item.

I have tried this but it didn't work

{if $product.users|fn_my_changes_get_product_orders} verified purchase {/if}

Try something like

{if $auth.user_id}
    {$params = [
        'user_id' => $auth.user_id,
        'p_ids' => $product.product_id
    ]}
    {$data = $params|fn_get_orders:0:true}
    {if $data.2}
         {$data.2}
    {/if}
{/if}

(!) Not tested

Try something like

{if $auth.user_id}
    {$params = [
        'user_id' => $auth.user_id,
        'p_ids' => $product.product_id
    ]}
    {$data = $params|fn_get_orders:0:true}
    {if $data.2}
         {$data.2}
    {/if}
{/if}

(!) Not tested

I tried it but it's not working

Create a function in my_changes/func.php like:

function my_check_previous_order($product_id, $user_id) {
if( empty($user_id) || empty($product_id) ) return false;
return db_get_field(“SELECT count(od.product_id) FROM ?:orders AS o LEFT JOIN ?:order_details AS od ON od.order_id=o.order_id AND od.product_id=?i WHERE o.user_id=?i”, $product_id, $user_id);
UNTESTED
}
And then in template:
{assign var=“ordered_before” value=$oi.product_id|my_check_previous_order:$auth.user_id}
[if $ordered_before}
verified order
{/if}
I updated the db_query above to use db_get_field instead.

I tried it but it's not working

Do you receive error or empty value?

Do you receive error or empty value?


Yes empty value

Create a function in my_changes/func.php like:

function my_check_previous_order($product_id, $user_id) {
  if( empty($user_id) || empty($product_id) ) return false;
  return db_get_field("SELECT count(od.product_id) FROM ?:orders AS o LEFT JOIN ?:order_details AS od ON od.order_id=o.order_id AND od.product_id=?i WHERE o.user_id=?i", $product_id, $user_id);
  UNTESTED
}
And then in template:
{assign var="ordered_before" value=$oi.product_id|my_check_previous_order:$auth.user_id}
[if $ordered_before}
  verified order
{/if}
I updated the db_query above to use db_get_field instead.

I tried this it's not showing verified purchase if it's not the user that left the feedback that's logged in, but i need even guests to be able to see the verified purchase

how can I check if a buyer purchased an item, I'm trying to show verified purchase on reviews that where left by users who purchased the item.

Your requirements are not clear. You reference 'buyer purchases' and then say you want guests to see. At this point I have no idea what you want. If you want further help, you'll have to request a quote via the link in my signature.

This is what i'm looking to do, on the review page to show verified purchase on reviews from users that bought the item


https://ibb.co/PW8MVtQ[image example]

It is not clear how do you want to get purchased items of anonymous customers

So what is a "verified purchase" to you? It doesn't really mean anything as text relative to the current user. If they didn't purchase, what to they care who else purchased but didn't leave a review?

Create a function in my_changes/func.php like:

function my_check_previous_order($product_id, $user_id) {
  if( empty($user_id) || empty($product_id) ) return false;
  return db_get_field("SELECT count(od.product_id) FROM ?:orders AS o LEFT JOIN ?:order_details AS od ON od.order_id=o.order_id AND od.product_id=?i WHERE o.user_id=?i", $product_id, $user_id);
  UNTESTED
}
And then in template:
{assign var="ordered_before" value=$oi.product_id|my_check_previous_order:$auth.user_id}
[if $ordered_before}
  verified order
{/if}
I updated the db_query above to use db_get_field instead.

This no longer works

I know of no changes that would affect the code I freely provided.

Yeah I'm trying to use this to tell the customer that they have purchased an item before and then give them a link to the order on the product page, e.g you purchased this item

Are you trying to reduce duplication and returns? Or are you just trying to be nice? If you just want to give them a warning, create a app/addons/my_changes/controllers/frontend/checkout.post.php file and put this content in it.

Click here to view the order.", fn_url("orders.details&order_id=$order_id") );
        fn_set_notification('W', __("warn"), $msg, 'K');
      }
    }
  }
  return array(CONTROLLER_STATUS_OK);
?>

**UNTESTED**

If you want to put the '$msg' on a product page instead, just wrap the above in a function tarting where the product_id is assigned and have it return $msg instead of setting the notification If no HIT then return an empty string.. Then your template code would look like (assuming you named this 'function my_purchased_before($product_id)'.

{$purchaed_before={$product.product_id|my_purchased_before}
{if $purchased_before}
  

{$purchased_before nofilter}

{/if}

You get the idea....

Yes, but I'm trying to show this as a note on the product page not a notification

If you can't get from there to where you want to be, then again, suggest you hire a developer to assist you.

I couldn't even get the alert to work, but thanks

I'm still working on understanding how all this works, hopefully i'd be able to get it to work, if not i can then look to hire someone when i have the funds, I appreciate your help

I managed to get the text to show "you bought this item", please how can i show the hink to the order in the product page?