Skip to content
Advertisement

Get WooCommerce product purchase date from a product Id for a user Id

I have faced a problem. I need to get a specific product purchase date using the user id and product id. Using Checking if customer has already bought something in WooCommerce answer code, it checks all product in the following:

JavaScript

What I need is to check specific product purchase data using the user id and product id.

How can I achieve this?

Advertisement

Answer

Using a direct custom SQL Query, avoiding looping through order items to find out the targeted product, will make the database query and the php process much more lighter and effective.

The query checks all product types, including variable products and product variations too.

Now for the date here we use the order post date.

So here is the custom query embedded in a function (returns a date or an empty value):

JavaScript

Code goes in function.php file of your active child theme (or active theme). Tested and works.


USAGE example (on current product page, for the current user Id):

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement