I'm trying to make stats script that makes clear usage for reservations of product for Woocommerce, diplaying available, reserved, and sold status. Status orders are available in backend admin panel, but I need to make count and visible in frontend.
Example:
ProductName
Items available: 10
Items already reserved (but not paid yet): X
Items sold: Y
Currently woocommerce has a function that shows sold quantity per product, but it counts both reserved (pending status) and sold (completed), so it's different kind of count.
I wrote a query that should find orders matching pending status (wp_term_relationships.term_taxonomy_id = 46) but I can't select only one specific product, as it counts all pending orders in database. The product ID is the same for wp_posts.ID and wp_woocommerce_order_itemmeta.meta_value = 250.
Note that product and orders are in the same table: wp_posts.
Here is my query:
SELECT COUNT(DISTINCT wp_posts.ID)
FROM wp_posts, wp_postmeta, wp_term_relationships, wp_woocommerce_order_itemmeta oi
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_posts.ID = wp_postmeta.post_id
AND oi.meta_key = '_product_id'
AND oi.meta_value = 250
AND wp_posts.post_type = 'shop_order'
AND wp_term_relationships.object_id = wp_posts.ID
AND wp_term_relationships.term_taxonomy_id = 46
Any ideas how to select count for only this one product? I feel I need just one more filer but can't figure out what is missing. Thanks in advance.
select ID, COunt(*) from .................. group by id
– Kiril Rusev