Skip to content

Instantly share code, notes, and snippets.

@resistorsoftware
Created December 1, 2010 20:06
Show Gist options
  • Select an option

  • Save resistorsoftware/724124 to your computer and use it in GitHub Desktop.

Select an option

Save resistorsoftware/724124 to your computer and use it in GitHub Desktop.
sum up orders for Shopify
sum = 0.0
page = 1
start_date = "2010-11-11"
end_date = "2010-12-11"
count = ShopifyAPI::Order.count({:fulfillment_status => 'unshipped', :created_at_max => "#{end_date} 23:59", :created_at_min => "#{start_date} 00:00"})
if count > 0
page += count.divmod(250).first
while page > 0
orders = ShopifyAPI::Order.find(:all, :params => {:page => page, :fulfillment_status => 'unshipped', :limit => 250, :created_at_max => "#{end_date} 23:59", :created_at_min => "#{start_date} 00:00", :financial_status => 'paid'})
orders.each do |order|
order.line_items.each do |item|
sum += item.price.to_f
end
page -= 1
end
end
@vgkids
Copy link
Copy Markdown

vgkids commented Dec 2, 2010

Thanks for posting this. Would you mind translating how the divmod method works? I get the function its performing in the script, but I'd love to learn what exactly is going on. I just looked up divmod in rubydocs and a little human translation would go a long way right now.

thanks!

@resistorsoftware
Copy link
Copy Markdown
Author

@mcapewell,

Thx.. I ripped this out of some app with other things on my mind... then edited it for the "gist" and not to "run" as is.. obviously I messed up my quickie edit...

@vgkids, Shopify only gives you 250 at a time. So if you had 1000 orders... you'd want 4 pages of 250. The divmod is one cheesy way to get the number of pages. I am sure there are more elegant ways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment