Skip to content

Instantly share code, notes, and snippets.

@seamusabshere
Created May 25, 2020 22:51
Show Gist options
  • Save seamusabshere/9d1016d0b6aa4c4c9b39933221ea718e to your computer and use it in GitHub Desktop.
Save seamusabshere/9d1016d0b6aa4c4c9b39933221ea718e to your computer and use it in GitHub Desktop.
Import orders into (paid) Shopify store
Thread.abort_on_exception = true
require 'bundler/setup'
require 'thread/pool'
require 'csv'
require 'shopify_api'
pool = Thread.pool 2
mutex = Mutex.new
# ActiveResource::Base.logger = Logger.new($stderr)
# ActiveResource::Base.logger.level = Logger::DEBUG
shop_url = "https://#{API_KEY}:#{PASSWORD}@#{SHOP_NAME}.myshopify.com/admin"
ShopifyAPI::Base.site = shop_url
ShopifyAPI::Base.api_version = '2020-04' # find the latest stable api_version [here](https://shopify.dev/concepts/about-apis/versioning)
count = 0
CSV.foreach(ARGV[0], headers: true) do |row|
row = row.to_hash
raw = {
name: row.fetch('id'),
referring_site: row.fetch('referring_site'),
processed_at: row.fetch('processed_at'),
line_items: [
{
title: row.fetch('line_items_title'),
price: row.fetch('line_items_price'),
quantity: row.fetch('line_items_quantity'),
}
],
billing_address: {
last_name: row.fetch('billing_address_last_name'),
address1: row.fetch('billing_address_address1'),
city: row.fetch('billing_address_city'),
zip: row.fetch('billing_address_zip'),
country: row.fetch('billing_address_country'),
province_code: row.fetch('billing_address_province_code'),
}
}
order = ShopifyAPI::Order.new raw
pool.process do
begin
sleep 0.3
order.save
mutex.synchronize do
count += 1
$stderr.write '+'
end
rescue ActiveResource::ClientError
if $!.message =~ /429/
mutex.synchronize do
$stderr.write '!'
end
retry
else
mutex.synchronize do
$stderr.puts $!.message
raise Exception
end
raise
end
end
end
end
pool.shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment