I need to know if a user has bought a WooCommerce product. And if is it possible how many order he did.
So I checked in the WordPress database, and find the “wp_woocommerce_order_items” table.
The order_item_id field is used to join this table with the products meta. I don’t find the reason for the order_id.
And I can’t understand how I can find the user who made the order.
As you can see in the second picture (wp_usermeta table) I don’t have no one field common with the order table.
So which is the common filed to join this 2 table?
I need only the field name that contain this info/value.
Virtual hugs for those who help me!
I just found the solutions for my own question! Thanks to everyone who answer me, you all helped me find the solution!
I didn’t know how WooCommerce save the order.
I just discover that the orders are saved using a post_type called shop_order.
So basically I find the user ID in the shop_order metas in wp_postmeta!
Here some screen with the SQL queries I did:
SELECT * FROM
wp_posts
WHERE post_type = ‘shop_order’SELECT * FROM
wp_postmeta
WHERE post_id = 1949Thank you all!!
Try this
This is basically from the template file
my-orders
. (woocommerce/templates/myaccount/my-orders.php). You can customize this code for your requirement. This gets you orders for the customer. You will need to changeget_current_user_id()
with the desired customers user id.Hope this helps.