programmatically creating products and variants... generates multiple images?

1,061 views
Skip to first unread message

Mel Adamei

unread,
Jun 19, 2012, 1:54:22 PM6/19/12
to spree...@googlegroups.com
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

Screen shot 2012-06-19 at 10.46.41 AM.png

Mel Adamei

unread,
Jun 20, 2012, 1:40:04 PM6/20/12
to spree...@googlegroups.com
Am I misunderstanding how product option values (etc) and variants work?  I thought maybe if I only used one instead of both, it would fix this issue.... anyone know what's up with how those two interplay?

sbeam

unread,
Jun 20, 2012, 4:18:28 PM6/20/12
to spree...@googlegroups.com
I can't quite parse your import script - maybe stating the problem as a series of simplified Rails console commands would help. Also, make sure you clearly state the problem - is it that you are getting duplicate images for some of the products, or multiples for all of them, or...?

That said, the problem may be that you are manually creating a Variant as well as a Product, and then attaching an image to either one. An image can only belong to Variant, and every Product has at least one Variant (the "master variant" - this is created in an after_initialize callback on Product). So not sure why your add_image function doesn't complain when passed an instance of Product, but the problem might be there. HTH

Mel Adamei

unread,
Jun 20, 2012, 4:26:29 PM6/20/12
to spree...@googlegroups.com
Basically, what's happening is this:

do this for all records in the db:
  if i can't find an existing spree product that matches the base_item_num of this new record
    create a product using certain values
    add taxons to this product
    add an image to this product
    add product properties to this product
    save this product and move on to the next record
  if i CAN find an existing spree product that matches the base_item_num of this new record
    create a variant using certain values, and link it to the matched spree product
    add taxons to the matched spree product
    add an image to the variant itself
    add this variant to the matched spree product's variants
    save the base product

is basically what's happening. :)

Mel Adamei

unread,
Jun 21, 2012, 12:49:47 PM6/21/12
to spree...@googlegroups.com
So, the problem lies with with base_prod.variants << variant.... not sure why though?

Is there a better way to add variants to a product that I'm not aware of?

Jean-Philippe Boily

unread,
Jun 21, 2012, 1:05:43 PM6/21/12
to spree...@googlegroups.com
I am not sure to understand your problem here, but you might want to try

base_prod.variants.build(:sku => item.sku,
  :price => item.price,
  :on_hand => item.on_hand)

OR

base_prod.variants.create(:sku => item.sku,
  :price => item.price,
  :on_hand => item.on_hand)


?


Jean-Philippe Boily |            j...@jipi.ca

Le jeudi 21 juin 2012 à 12:49, Mel Adamei a écrit :

base_prod.variants

Mel Adamei

unread,
Jun 21, 2012, 1:08:33 PM6/21/12
to spree...@googlegroups.com
I'll give it a try right now! :D

Mel Adamei

unread,
Jun 21, 2012, 1:43:17 PM6/21/12
to spree...@googlegroups.com
Doesn't work, I tried it in many combinations. :(

Is there a better way you think I can attempt to explain this?

When I add the variant to the product, it seems to add other images to it as well.  It seems to add the images of the next product created, and then that product then takes the product next after that, etc.... which I really don't understand why or how that could even happen. :S

It's really kind of bizarre, and if I can't solve it in the next week or so here at work, we're probably going to have to start looking for a different solution other than Spree. :(

Jean-Philippe Boily

unread,
Jun 21, 2012, 4:08:24 PM6/21/12
to spree...@googlegroups.com
Can you re-post your code in a gist (http://gist.github.com) please and maybe explain again what is your intent and what is the exact problem?

Jean-Philippe Boily |            j...@jipi.ca

--
You received this message because you are subscribed to the Google Groups "Spree" group.
To view this discussion on the web visit https://groups.google.com/d/msg/spree-user/-/kuBfgTXKXJ8J.
To post to this group, send email to spree...@googlegroups.com.
To unsubscribe from this group, send email to spree-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spree-user?hl=en.

Reply all
Reply to author
Forward
0 new messages