Skip to content

Instantly share code, notes, and snippets.

Created March 4, 2010 16:44
Show Gist options
  • Save anonymous/321889 to your computer and use it in GitHub Desktop.
Save anonymous/321889 to your computer and use it in GitHub Desktop.
namespace :spree do
desc "Update / Import products from CSV File, expects file=path/to/file.csv"
task :import_products => :environment do
require 'fastercsv'
n = 0
u = 0
FasterCSV.foreach(ENV['file']) do |row|
if row[0].nil?
# Adding new product
puts "Adding new product: #{row[1]}"
product = Product.new()
n += 1
else
# Updating existing product
next if row[0].downcase == "id" #skip header row
puts "Updating product: #{row[1]}"
product = Product.find(row[0])
u += 1
end
product.name = row[2]
product.available_on = Time.now
product.description = row[3]
# product.permalink = row[6].to_s
product.vendor_sku = row[12]
product.price = row[13]#.to_d
product.save(false)
end
puts ""
puts "Import Completed - Added: #{n} | Updated #{u} Products"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment