Thursday, 11 May 2017

Fetch all products from Shopify api



Suppose, If your store contains more than 50 products and you wish to fetch all products from store.

When you call a shopify api's like this -

products = ShopifyAPI::Product.all

Above api will fetch maximum of 50 products but you can use limit parameter to set limit of products in every call of api's like this -

products = ShopifyAPI::Product.find(:all,:params => {:limit => 250})

But above api's also returns only 250 products. What if you have more then 250? Can’t you just increase the limit to the total number of products in your catalog to 1000? As simple as it sounds, it doesn’t quite work in that way… Shopify uses a technique called pagination, which basically divides the response to your request into separate memory chunk.

So, we can use pagination technique to fetch all products from shopify -
count = ShopifyAPI::Product.count
       page = 1
       products = []
       while count > 0 do
         products << ShopifyAPI::Product.find(:all, :params => {:page => page})
         count = count - 50
         page = page + 1
       end
 products.flatten!

It will return all products of your shopify store.

3 comments:

  1. Hire Shopify developers from GEEKS PER HOUR who are adept at Shopify development trends. Our Shopify experts provide consistent and cost-effective solutions to fill the gaps in your Shopify project while ensuring complete transparency and control.

    ReplyDelete
  2. A personal course includes the establishment of a professional store accompanied by personal training and professional consultation. shopify

    ReplyDelete
  3. Thanks for sharing this informative article on Shopify app development. If you have any requirement to Hire Shopify Developers for your project. You can contact us for more information.

    ReplyDelete