Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

How to filter orders by particular product id in get orders api

Solved

How to filter orders by particular product id in get orders api

ArunRaj
Tourist
8 0 2

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?

Accepted Solution (1)

HunkyBill
Shopify Partner
4853 60 568

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.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com

View solution in original post

Replies 10 (10)

HunkyBill
Shopify Partner
4853 60 568

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.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
ArunRaj
Tourist
8 0 2

@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.

HunkyBill
Shopify Partner
4853 60 568

Probably not. What have you tried so far?

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
ArunRaj
Tourist
8 0 2

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.

HunkyBill
Shopify Partner
4853 60 568

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. 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
ArunRaj
Tourist
8 0 2

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.

 

tungnguyen
Tourist
4 0 1

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

HunkyBill
Shopify Partner
4853 60 568

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:

 

  • hey order, did any of these 5000 products sell here?
  • YES! Ok... create a hash keyed on the ID and bump a counter

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.

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
tungnguyen
Tourist
4 0 1

Thanks HunkyBill. You are very funny. I was hoping it was a easier process than this! 

Cheers,
Tung

shrshk7
Shopify Partner
1 0 0

lol I love the ending of your comment (dead emoji)