Created
January 14, 2014 19:25
-
-
Save saranyan/8424148 to your computer and use it in GitHub Desktop.
Bigcommerce options tutorial (Pizza store)
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
thin | http://bevcooks.com/wp-content/uploads/2011/12/pizza9.jpg | 0 | |
---|---|---|---|
hand stuffed | http://cdn.foodbeast.com.s3.amazonaws.com/content/uploads/2013/10/3-cheese-closeup.jpg | 0 | |
regular | http://blogs.babble.com/family-kitchen/files/2010/10/Stuffed-Crust-Pizza.jpg | 0 |
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
require 'rest_client' | |
require 'csv' | |
require 'json' | |
require 'base64' | |
creds = {"username"=>"admin", "apikey"=>"xxxxxxxxxx", "url"=>"https://xxxxx.mybigcommerce.com/api/v2/"} | |
files = {"toppings" => 'toppings.csv', "crusts" => 'crust.csv', "products" => 'pizzas.csv'} | |
url = "https://"+creds["username"]+":"+creds["apikey"]+"@"+creds["url"].gsub('https://','') | |
optionsets = ["toppings","crusts"] | |
optionsets_ids = [] | |
store_optionset_names = ["toppings","crusts"] | |
store_optionsets = [] | |
pizza_category_id = 0 | |
#Clearing all optionsets, options, products | |
p "Deleting products, categories, options and optionsets from your store" | |
RestClient.delete("#{url}optionsets.json") | |
RestClient.delete("#{url}options.json") | |
RestClient.delete("#{url}products.json") | |
RestClient.delete("#{url}categories.json") | |
RestClient.get("#{url}optionsets.json", { :params => {}, :accept => :json}) { |response, request, result, &block| | |
case response.code | |
when 200 | |
store_optionsets = JSON.parse(response) | |
store_optionset_names = store_optionsets.collect{|x| x["name"]} | |
else | |
nil | |
end | |
} | |
RestClient.get("#{url}categories.json", { :params => {:name => "pizza"}, :accept => :json}) { |response, request, result, &block| | |
case response.code | |
when 200 | |
temp = JSON.parse(response) | |
if temp.is_a? Array | |
pizza_category_id = Integer(temp[0]["id"]) | |
else | |
pizza_category_id = Integer(temp["id"]) | |
end | |
else | |
res = RestClient.post("#{url}categories.json",{ "name" => "pizza"}.to_json, :content_type => :json, :accept => :json){ |response, request, result, &block| | |
case response.code | |
when 201 | |
pizza_category_id = Integer(JSON.parse(response)["id"]) | |
else | |
nil | |
end | |
} | |
end | |
} | |
csv = CSV.read(files["toppings"], :headers=>false, :encoding => "ISO-8859-1") | |
#create optionsets | |
#toppings, #crusts | |
optionsets.each do |os| | |
res = RestClient.post("#{url}optionsets.json", { "name" => os}.to_json, :content_type => :json, :accept => :json) { |response, request, result, &block| | |
case response.code | |
when 201 | |
p "Created an option set #{os}" | |
optionsets_ids << Integer(JSON.parse(response)["id"]) | |
when 409 | |
index = store_optionset_names.index(os) | |
p "Found an option set with ID #{store_optionsets[index]["id"]}" | |
optionsets_ids << Integer(store_optionsets[index]["id"]) | |
else | |
response.return!(request, result, &block) | |
end | |
} | |
end | |
csv.each_with_index do |c,i| | |
topping = c[0] | |
res = RestClient.post("#{url}options.json", { "name" => topping, "display_name" => topping, "type" => "C" }.to_json, :content_type => :json, :accept => :json) { |response, request, result, &block| | |
case response.code | |
when 201 | |
p "Created topping #{topping}" | |
#add this to topping optionset | |
oid = optionsets_ids[store_optionset_names.index("toppings")] | |
option_id = Integer(JSON.parse(response)["id"]) | |
RestClient.post("#{url}optionsets/#{oid}/options.json", { "option_id" => option_id }.to_json, :content_type => :json, :accept => :json) { |response, request, result, &block| | |
case response.code | |
when 201 | |
p "Added #{topping} to the optionset ID #{oid}" | |
else | |
response.return!(request, result, &block) | |
end | |
} | |
when 409 | |
p "Skipping...resource already exists" | |
else | |
response.return!(request, result, &block) | |
end | |
} | |
end | |
csv = CSV.read(files["crusts"], :headers=>false, :encoding => "ISO-8859-1") | |
crust_products_id = [] | |
crust_products_names = [] | |
csv.each_with_index do |c,i| | |
name_of_crust = c[0] | |
image_url = c[1] | |
#we need to create products with these names and images | |
res = RestClient.post("#{url}products.json", { "name" => name_of_crust, "categories" => [pizza_category_id], "type" => "physical", "weight" => 0.0, "price" => 0.0, "availability" => "disabled" }.to_json, :content_type => :json, :accept => :json) { |response, request, result, &block| | |
case response.code | |
when 201 | |
crust_products_id << JSON.parse(response)["id"] | |
crust_products_names << JSON.parse(response)["name"] | |
img_resource = JSON.parse(response)["images"]["resource"] | |
#splice first slash | |
img_resource[0] = '' | |
#p "created product #{name_of_crust}. attempted to add image #{(image_url.gsub(/^\s+/,''))} #{url}#{img_resource}.json" | |
RestClient.post("#{url}#{img_resource}.json", { "image_file" => (image_url.gsub(/^\s+/,''))}.to_json, :content_type => :json, :accept => :json) { |response, request, result, &block| | |
case response.code | |
when 201 | |
p "added image to crust product #{name_of_crust}" | |
else | |
response.return!(request, result, &block) | |
end | |
} | |
else | |
response.return!(request, result, &block) | |
end | |
} | |
end | |
#create an option called crust with all the crust products and add it to option_set "toppings" | |
res = RestClient.post("#{url}options.json", { "name" => "crust", "display_name" => "Pizza crust", "type" => "PI" }.to_json, :content_type => :json, :accept => :json) { |response, request, result, &block| | |
case response.code | |
when 201 | |
#add those products to values | |
val_url = JSON.parse(response)["values"]["resource"] | |
val_url[0] = '' | |
oid = optionsets_ids[store_optionset_names.index("toppings")] | |
option_id = Integer(JSON.parse(response)["id"]) | |
#p "created options called crust. attempted to add product values to it #{url}#{val_url}.json" | |
crust_products_id.each_with_index do |cp,i| | |
RestClient.post("#{url}#{val_url}.json", { "label" => crust_products_names[i], "value" => cp}.to_json, :content_type => :json, :accept => :json) { |response, request, result, &block| | |
case response.body | |
when 201 | |
p "Successfully added product ID #{cp} to option called crusts" | |
else | |
response.return!(request, result, &block) | |
end | |
} | |
end | |
RestClient.post("#{url}optionsets/#{oid}/options.json", { "option_id" => option_id }.to_json, :content_type => :json, :accept => :json) { |response, request, result, &block| | |
case response.code | |
when 201 | |
p "Added crust to the optionset ID #{oid}" | |
else | |
response.return!(request, result, &block) | |
end | |
} | |
when 409 | |
p "Skipping...resource already exists" | |
else | |
response.return!(request, result, &block) | |
end | |
} | |
csv = CSV.read(files["products"], :headers=>false, :encoding => "ISO-8859-1") | |
csv.each_with_index do |c,i| | |
name = c[0] | |
image_url = c[1] | |
price = c[2] | |
oid = optionsets_ids[store_optionset_names.index("toppings")] | |
res = RestClient.post("#{url}products.json", { "name" => name, "categories" => [pizza_category_id], "type" => "physical", "weight" => 0.0, "price" => price, "availability" => "available", "is_visible" => true, "option_set_id" => oid }.to_json, :content_type => :json, :accept => :json) { |response, request, result, &block| | |
case response.code | |
when 201 | |
img_resource = JSON.parse(response)["images"]["resource"] | |
#splice first slash | |
img_resource[0] = '' | |
RestClient.post("#{url}#{img_resource}.json", { "image_file" => (image_url.gsub(/^\s+/,''))}.to_json, :content_type => :json, :accept => :json) { |response, request, result, &block| | |
case response.code | |
when 201 | |
p "added image to product #{name}" | |
else | |
response.return!(request, result, &block) | |
end | |
} | |
else | |
response.return!(request, result, &block) | |
end | |
} | |
end |
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
small | http://www.greenkitchenstories.com/wp-content/uploads/2011/06/Mini_pizza.jpg | 9.99 | |
---|---|---|---|
medium | http://static.wixstatic.com/media/a2c2fe_9cb8ddabcbeb817952259813a784a04b.jpg | 12.99 | |
large | http://www.laitaliankitchen.com/wp-content/themes/Gourmet/thumb.php?src=http://www.laitaliankitchen.com/wp-content/uploads/2010/09/LargeOneTopping1.png&w=590&zc=1&q=80&bid=1 | 19.99 | |
extra-large | http://2.bp.blogspot.com/_X052DPd66Yg/S-oyc1HKg2I/AAAAAAAAAS0/x98b9ht5Pw4/s400/100_4564.JPG | 29.99 |
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
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
tomatoes | |
onions | |
jalapenos | |
pineapple | |
black olives | |
peppers | |
artichokes | |
ham | |
sausage | |
chicken | |
pepperoni | |
bacon |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment