Last active
October 7, 2017 08:39
-
-
Save amitpatelx/9046592c3d610c7b4220230d39165074 to your computer and use it in GitHub Desktop.
How to fetch all products using Shopify’s Ruby SDK?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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