Shopify API: Retrieve multiple records via id in a single call
Asked Answered
R

3

6

I noticed that in the Shopify API documentation, they mention the possibility to retrieve multiple orders in a single call using "A comma-separated list of order ids" as a parameter called "ids".

Link to section of docs I'm referring to: https://docs.shopify.com/api/order#index

I've been using the shopify_api gem for many years, which is based around Rails ActiveResource. I currently use it with Rails 3.2.13, and it works great.

I know how to retrieve a single record:

# params[:id] = "123456789"
order = ShopifyAPI::Order.find(params[:id])

Or many records at once:

orders = ShopifyAPI::Order.find(:all, :params => {:limit => 250, :page => 2})

However, I cannot seem to get it to work using multiple ids. Any ideas what I am doing wrong here?

# params[:ids] = "123456789,987654321,675849301"  
orders = ShopifyAPI::Order.find(:all, :params => {:ids => params[:ids]})

Which issues this GET request: https://xxxxxx.myshopify.com:443/admin/orders.json?ids=123456789,987654321,675849301

But gives nothing back, orders = []

UPDATE:

I've also tried the following suggestions:

# params[:ids] = "123456789,987654321,675849301"  
orders = ShopifyAPI::Order.find(params[:ids])

Which issues this GET request: https://xxxxxx.myshopify.com:443/admin/orders/123456789,987654321,675849301.json

However this only returns the first order 123456789

And:

# params[:ids] = "123456789,987654321,675849301"
ids_as_array = params[:ordersSel].split(",")
orders = ShopifyAPI::Order.find(:all, :params => {ids: ids_as_array})

Which issues this GET request: https://xxxxxx.myshopify.com:443/admin/orders.json?ids[]=123456789&ids[]=987654321&ids[]=675849301

And results in a Bad Request

Rectrix answered 14/9, 2015 at 7:34 Comment(0)
D
4

This works for me, just tested it out in the console

myids = "2354899011,1234263747"
ShopifyAPI::Order.where(ids: myids)

This results in the following request

https://myfancyshop.myshopify.com/admin/orders.json?ids=2354899011,1234263747

Also this ShopifyAPI::Order.find(:all, :params => { :ids => myids }) provides me with the same result.

Dextrorse answered 14/9, 2015 at 14:54 Comment(6)
This solution does indeed work but I see that you've already tried similar things or exactly the same. Are you sure that all the IDs are in the shop? If not the result will be nilDextrorse
Thanks @voskart... then at least I'm not crazy :) May I ask what version of Rails + Shopify Api gem you are using? Tried those exact combinations without luck earlier, and all the order ids are from the shopRectrix
@BjornForsberg I'm using rails 4.2.0 and for the shopify_app 6.2.0 (shopify_api is 4.0.6)Dextrorse
Thanks @Dextrorse Spent most of the day upgrading from Rails 3.2 to 4.2 only to find out the issue was actually that the Orders were Archived, and therefore I need to include the :status => 'any' option for them to show up. Thanks for the help anyway :/Rectrix
Glad you fixed it @BjornForsbergDextrorse
Are you still developing shopify apps? I cant seem to fetch orders AT ALL anymore when i was able to last october.Rondarondeau
R
2

Ok, so what I had been trying was actually correct. The problem turns out to be that my orders in the test store are Archived/Closed and so the request needs to be like this:

# params[:ids] = "123456789,987654321,675849301"  
orders = ShopifyAPI::Order.find(:all, :params => {
    :ids => params[:ids],
    :status => 'any'
    })

The key is to include the :status => 'any' in the call otherwise the API only returns Orders which have the status 'Open'.

Note: The above works on Rails 3.2 and Shopify API gem 3.2.6 (so no need to be using the latest versions).

Thanks to both of the other answers for confirming this was possible.

Rectrix answered 15/9, 2015 at 12:55 Comment(0)
G
0

You can pass the ids as the first parameter of find:

orders = ShopifyAPI::Order.find(params[:ids])

or if you need the paging:

orders = ShopifyAPI::Order.find(params[:ids], :params => {:limit => 250, :page => 1})
Garwin answered 14/9, 2015 at 7:40 Comment(5)
Thanks, but unfortunately that does not work either @Garwin :( It only returns the first record in the list. I've updated my question to show this now.Rectrix
Hm, how about orders = ShopifyAPI::Order.where(id: params[:ids])?Garwin
ActiveResource does not have a "where" method, so that does not work ;) Thanks anyway @GarwinRectrix
Ok, last guess. Does it help if you split the params? params[:ids].split(',').Garwin
Appreciate the help @zwippie, but unfortunately that results in "Bad Request" :(Rectrix

© 2022 - 2024 — McMap. All rights reserved.