I'm having this weird issue where I'm pretty sure I'm doing something wrong, but I'm not sure what! :)
I've completely re-written the code from scratch so that it's less of a hodge-podge of junk, but it's still doing it!
I've attached a screenshot of what I mean, too. Where the product says "base" it means it's outputting that picture in the loop for the main product, and where it says "variant" it's outputting that picture in the loop for the product's variants. That's the only modification I've made to this page (products/_thumbnails.html.erb), other than the layout theme has been changed a bit.
You can see that they "look for similar items" repeats some stuff... this is because when I add the taxon category things, I don't check to make sure it's already included... when a variant is added, it adds on the extra taxons for that variant (because they have some extra information, mostly finish).
Here's the main loop that adds a products (base_item_num is how I detect if it's a variant from the DB I'm pulling from... if they are the same, then they are variants of each other.), and the function that adds the image.
Can anyone see anything wrong? :S
def run(query)
print "\nRunning...\n"
# Save it locally so we don't keep getting kicked out, plus performance increase?
results = XologicItem.find_by_sql(query)
results.each do |item|
row(item)
end
return -1
end
def row(item)
print "."
base_prod = Spree::Product.find_by_base_item_num(item.base_item_num)
if base_prod.nil? || base_prod.blank?
product = Spree::Product.create!(:name => item.name.titleize,
:price => item.price,
:base_item_num => item.base_item_num,
:description => item.description,
:sku => item.sku,
:height => item.height,
:width => item.width,
:depth => item.depth,
:weight => item.weight,
:on_hand => item.on_hand,
:available_on => Time.now)
parse_taxons(item, product) # Just adds the taxons
add_image(item, product)
propertyize(item, product) # Just adds the properties
product.save
else
variant = Spree::Variant.create!(:product_id => base_prod,
:sku => item.sku,
:price => item.price,
:on_hand => item.on_hand)
# Add the taxons to the base product, not the variant.
parse_taxons(item, base_prod)
add_image(item, variant)
base_prod.variants << variant
base_prod.save
end
end
def add_image(item, thing)
unless item.image_uri.nil?
image = Spree::Image.create!({:attachment => open(URI.parse(item.image_uri)),
:viewable => thing}, :without_protection => true)
thing.images << image # tried adding this... didn't change anything.
end
end