How can I list order items based on a specific product variations?
Given a variation ID, I would like to generate a list of all order items that include that particular variation.
In my case I'm using variations to represent dates and the product itself is a recurring event, so the purpose of this is to list the people attending that specific date.
It would be amazing if this could be accomplished by something simple like a get_posts
using meta_keys
or something in that vein, but otherwise I'm guessing a custom query would be the way.
I just can't seem to figure out how the tables relate in this case or if this is stored in a searchable way.
Any help very much appreciated.
Thanks!
I tried to do the same, after looking at db I was getting
0
in front of'_variation_id'
. But got the orders items using my custom hack for the future people who are looking for the same issue. We need to compare usingproduct_name
,variaion_name
such as Size etc. Andvariation_value
LIKE 'S', 'L' etc. Consider this function:Now call it, say our product name is
Raleigh sport
, variation name issize
and value iss
. In simple form, we want to get the order items id of the product whose name isRaleigh sport
and we want to fetch only whosesize
isS
simple call by function:
print_r(get_all_orders_that_have_a_product_variation( "Raleigh Sport", "Size", "S" ));
thanks.
There is many ways to accomplish that. Here I use in a custom function with one SQL query and a
$variation_id
(the variation ID to set in) as parameter in it:Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
USAGE (here with the variation ID 41 for example):
This will display a list of orders items IDs for this variation ID with some data (for example).
This code is tested and works.
Now if you need to get all the Orders IDs instead, you can use another query inside that function this way:
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
USAGE (here always with the variation ID 41 for example):
This will display a list of orders IDs for this variation ID with their status (for example).
This code is tested and works.