Skip to content

Instantly share code, notes, and snippets.

@amitpatelx
Last active October 7, 2017 08:39
Show Gist options
  • Save amitpatelx/9046592c3d610c7b4220230d39165074 to your computer and use it in GitHub Desktop.
Save amitpatelx/9046592c3d610c7b4220230d39165074 to your computer and use it in GitHub Desktop.
How to fetch all products using Shopify’s Ruby SDK?
# assumption: configured as per https://github.com/Shopify/shopify_app#managing-api-keys
class Shopify::ProductService
def new
# get valid session to connect with the target store
session = ShopifyAPI::Session.new("your shop shopify_domain", "your shop shopify_token")
ShopifyAPI::Base.activate_session(session)
end
def products
products = [] # Collect all products in array
(1..pages).each do |page|
products += ShopifyAPI::Product.all(params: { page: page, limit: PER_PAGE })
end
end
private
PER_PAGE = 250.0
# calculates total number of pages required
def pages
(ShopifyAPI::Product.count / PER_PAGE).ceil
end
end
# Get all products
Shopify::ProductService.new.products
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment