there is something strange going on with the Shopify API, I cannot understand what I am doing wrong.
I’d like to get from Shopify a list of all the products so I use the following code:
def get_all_products_from_shopify
limit = 250
all_products = Array.new
self.connect_to_store
products = ShopifyAPI::Product.find(:all, :params => {:limit => limit})
all_products = all_products.concat products
puts products.length
while products.length == limit do
since_id = products.last.id
products = ShopifyAPI::Product.find(:all, :params => {:limit => limit, :since_id => since_id})
all_products = all_products.concat products
end
ShopifyAPI::Base.site = nil
return all_products
end
The issue is that I have 251 products on Shopify but with this method I retrieve an array of products that has a size of 277 elements.
Why?
Also if I do:
products = ShopifyAPI::Product.count
> 251
products = ShopifyAPI::Product.find(:all, :params => {:limit => limit})
products.count
>250
since_id = products.last.id
ShopifyAPI::Product.count(since_id: since_id)
>26
Can anybody tell me what am I doing wrong?
Thanks,
Augusto
By default products are returned in ascending order by title in the product list API. Instead of omitting the
:since_idparam for the first page of results, use:since_id => 0.