Created
December 1, 2010 20:06
-
-
Save resistorsoftware/724124 to your computer and use it in GitHub Desktop.
sum up orders for Shopify
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
| 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 |
Haven't run the code but it looks like lines 14 and 15 should be swapped around...
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!
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
Note that there is no provision to sleep while waiting for the next 300 API calls... in 600 seconds... that is an exercise for another day for whoever is into this kind of stuff...