Discussing APIs and development related to customers, discounts, and order management.
Hello,
We have one requirement to filter out orders for given product id list. So is there any API available to get orders for particular line items?
Solved! Go to the solution
This is an accepted solution.
Rest API
Step 1:
- get orders
Step 2:
- iterate line items in those orders looking for product that matches, build your results
GraphQL
- ask for order with a line item fragment that would then allow you to try and match on product ID
I would think the GraphQL would be the least resource intensive approach, but you need to be on your A game to get that working. The Rest API version is the pokey old no-brainer, guaranteed to work, but will take some time to product your list.
This is an accepted solution.
Rest API
Step 1:
- get orders
Step 2:
- iterate line items in those orders looking for product that matches, build your results
GraphQL
- ask for order with a line item fragment that would then allow you to try and match on product ID
I would think the GraphQL would be the least resource intensive approach, but you need to be on your A game to get that working. The Rest API version is the pokey old no-brainer, guaranteed to work, but will take some time to product your list.
@HunkyBill Except API is there any way to filter orders by product in Shopify Admin orders list page so that I can redirect to orders page.
Probably not. What have you tried so far?
I am trying to get orders using GraphQL since filter orders using REST API solution is time consuming one. Why I asked this is to select all orders and customers we are redirecting users to admin page only. So if this filter option possible we can redirect to orders page for particular product.
Who are you redirecting? Why? What is the purpose? Making a filter on orders is not a big deal... you probably want to accept a date range, and then a product choice, and go from there. A simple solution for you might be to listen to order webhooks, and when they come in, build a simple index in a database. A dead simple query could then almost instantly show you which orders had which products, no fuss, no muss.
We have added bulk action in orders page for our app. That's how we get orders id and performs action and we are already using orders webhook also to perform action whenever order is created.
@HunkyBill wrote:you probably want to accept a date range, and then a product choice, and go from there.
Yes, we are going to implement this only. Just making sure is there a way to filter orders by product in orders page. Thank you for your reply.
Hi guys,
Has anyone managed to get this working? We are trying to do the similar thing. So, provided a list of 5,000 product ids, we need to figure out
- which products have been purchased.
- which products have never been purchased.
The closest thing that I can get with GraphQL is:
{ orders(first:5) { edges { node { lineItems(first: 5) { edges { node{
sku product { id title } } } } } } } }
But I can't figure out how we can filter based on the product ids. I have tried different things:
- Filtering product id on lineItems: Field 'lineItems' doesn't accept argument 'query'
- Filtering product id on product: Field 'product' doesn't accept argument 'id'
I would expect a query like product_id: ["123", "456", "789"]
Any suggestion?
Cheers,
Tung
The details of the problem you're working on, no matter what API you choose to use is the fact, "FUN FACT infact", that going through orders and looking for 5000 products that sold is only a part of the work. That part is a no brainer algorithm:
But then you have to remember, a lot A LOT A LOT of products get returned. So now you have to go through all the transactions associated with an order, and look for the self same 5000 products, and remove returns from your counter. Some products that sold will now disappear from the list of sold products.
Happy computing. Never say it takes too long. It is a fricking computer after all. Silicon and electricity. It is not like you carrying or pushing a 1 ton rock up a hill in your latex sandals under a hot sun with no water breaks and a mean person whipping your back with horse hairs when you take a break.
Thanks HunkyBill. You are very funny. I was hoping it was a easier process than this!
Cheers,
Tung
lol I love the ending of your comment (dead emoji)