we are developing a complex Woocommerce website and an app which uses V3 API.
Being sold products very valuable, we need to know the ordinal number of a purchase for each product. For example, we'd like to know that Alice was the third to buy that product and Bob the fifth and so on. What I can think of is to get the total sales through Woocommerce API for that product, then loop every order containing that product and order them by date. The problem is that this way is very time and resources consuming, especially having to loop through all the orders. Does anybody know a better approach. I don't ask for the code, but I'd be glad if someone could have a brilliant approach to share.
Thanks! Fabio
we are developing a complex Woocommerce website and an app which uses V3 API.
Being sold products very valuable, we need to know the ordinal number of a purchase for each product. For example, we'd like to know that Alice was the third to buy that product and Bob the fifth and so on. What I can think of is to get the total sales through Woocommerce API for that product, then loop every order containing that product and order them by date. The problem is that this way is very time and resources consuming, especially having to loop through all the orders. Does anybody know a better approach. I don't ask for the code, but I'd be glad if someone could have a brilliant approach to share.
Thanks! Fabio
Share Improve this question asked Jul 4, 2020 at 19:23 Fabio RicciFabio Ricci 1032 bronze badges1 Answer
Reset to default 1Assuming you are not allowed guest checkout what you can try is
- add a custom field to your WooCommerce products in which you would save the history of purchases as an array like
[<user_id_1>, <user_id_2>, ...]
; - write a custom function and use a
woocommerce_order_status_completed
hook to update this field according to products within the order (see this question for information on how to get user ID from the WooCommerce order); - write a function which will do a loop through all already placed orders and initialize that field value for each of your products.
This can be done as a plugin where initialization of this custom field will occur during plugin activation. And with the same plugin you could register an additional REST API endpoint.