Re: Below code to implement pagination is not working while fetching new orders from Shopify.

Below code to implement pagination is not working while fetching new orders from Shopify.

rakesh11
Visitor
2 0 0
LIMIT=250

def fetch_shopify_orders(status="paid") 
ShopifyAPI::Order.all(session: @session, financial_status: status, updated_at_min: Time.current.to_date - 2.days, limit: LIMIT)
end

def fetch_next_shopify_orders 
ShopifyAPI::Order.all(session: @session, page_info: ShopifyAPI::Order.next_page_info, limit: LIMIT)
end

orders = fetch_shopify_orders
while ShopifyAPI::Order.next_page?
orders = fetch_next_shopify_orders
end

 

I have taken reference from here https://shopify.github.io/shopify-api-ruby/usage/rest.html.

Using configuration mentioned below.
ruby '2.6.10'
gem 'rails', '> 5.1.6'
gem 'shopify_api', '> 12.5.0'

Please help me to resolve this issue, any help would be highly appreciated.

Replies 3 (3)

SBD_
Shopify Staff
1831 273 418

Hey @rakesh11 

 

Do you get a specific error? What's the output? Have you confirmed the store has multiple pages of orders?

Scott | Developer Advocate @ Shopify 

rakesh11
Visitor
2 0 0

Hi 

 

Not getting any error, the above query returns top 250 orders and after that it does not return the next 250 orders.

 

Yes there are thousands of orders in store. I also tried to check this variable "ShopifyAPI::Order.next_page?" and it returns false but there are many orders below top 250 orders.

 

The script which is mentioned above should return all orders in one execution, but unfortunately it is not happening for me.

 

Thanks

SBD_
Shopify Staff
1831 273 418

Hey @rakesh11 

 

Can you give this code a shot?

 

orders = ShopifyAPI::Order.all(session: session, limit: 250)

loop do
  puts orders
  break unless ShopifyAPI::Order.next_page?
  orders = ShopifyAPI::Order.all(session: session, limit: 250, page_info: ShopifyAPI::Order.next_page_info)
end

Scott | Developer Advocate @ Shopify