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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bjorn Forsberg
  • 470
  • 1
  • 6
  • 19

3 Answers3

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.

voskart
  • 767
  • 1
  • 7
  • 15
  • 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 nil – voskart Sep 14 '15 at 15:21
  • 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 shop – Bjorn Forsberg Sep 14 '15 at 17:10
  • @BjornForsberg I'm using rails 4.2.0 and for the shopify_app 6.2.0 (shopify_api is 4.0.6) – voskart Sep 15 '15 at 07:56
  • Thanks @voskart 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 :/ – Bjorn Forsberg Sep 15 '15 at 12:57
  • Glad you fixed it @BjornForsberg – voskart Sep 15 '15 at 13:05
  • Are you still developing shopify apps? I cant seem to fetch orders AT ALL anymore when i was able to last october. – uno Mar 04 '19 at 00:45
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.

Bjorn Forsberg
  • 470
  • 1
  • 6
  • 19
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})
zwippie
  • 15,050
  • 3
  • 39
  • 54