Cannot create Variants in Admin after programmatic product import

40 views
Skip to first unread message

Jessa Denmark

unread,
Jun 18, 2016, 8:34:40 PM6/18/16
to Spree
I have defined Option Types, Option Values, and several products programmatically.  Now, in the Admin, I see the message "To add variants, you must first define Option Types and Option Values" on the Manage Variants page.  I am guessing that there is data somewhere that is not setup correctly and keeping Spree from recognizing the Options.

What can I do to correct this?

On the customer-facing website the products work correctly.

Here is a sample of the code that generated things:


tcw
= Spree::OptionType.where(:name => "tcw", :presentation => "Total Carat Weight").first_or_create
metal
= Spree::OptionType.where(:name => "Metal", :presentation => "Metal").first_or_create
width
= Spree::OptionType.where(:name => "Width", :presentation => "Width").first_or_create

PLT
= Spree::OptionValue.where(:option_type => metal, :name => "PLT", :presentation => "Platinum").first_or_create
RG14K
= Spree::OptionValue.where(:option_type => metal, :name => "RG14K", :presentation => "Rose Gold 14K").first_or_create
RG18K
= Spree::OptionValue.where(:option_type => metal, :name => "RG18K", :presentation => "Rose Gold 18K").first_or_create
WG14K
= Spree::OptionValue.where(:option_type => metal, :name => "WG14K", :presentation => "White Gold 14K").first_or_create
WG18K
= Spree::OptionValue.where(:option_type => metal, :name => "WG18K", :presentation => "White Gold 18K").first_or_create
YG14K
= Spree::OptionValue.where(:option_type => metal, :name => "YG14K", :presentation => "Yellow Gold 14K").first_or_create
YG18K
= Spree::OptionValue.where(:option_type => metal, :name => "YG18K", :presentation => "Yellow Gold 18K").first_or_createe  

product_DOJ2034
= Spree::Product.create!(
 
:name => "The Ginnie Ring".titleize,
 
:description => "This graceful ring is sculpted with an entwining band, holding the the precious center stone.",
 
:available_on => Time.now,
 
:price => 680,
 
:sku => "STYLE-DOJ2034",
 
:shipping_category => shipping_category, :tax_category => taxcategory,

)

var_14889
= Spree::Variant.create(
 
:product => product_DOJ2034,
 
:sku => "14889",
 
:price => 680,
 
:cost_price => 450,

 
:track_inventory => false,

 
:width => 2.5,
 
:depth => nil,
 
:height => nil,
 
:weight => 0,
 
:option_values => [WG14K],
)

var_14890
= Spree::Variant.create(
 
:product => product_DOJ2034,
 
:sku => "14890",
 
:price => 780,
 
:cost_price => 517,

 
:track_inventory => false,

 
:width => 2.5,
 
:depth => nil,
 
:height => nil,
 
:weight => 0,
 
:option_values => [WG18K],
)


omair azam

unread,
Jun 18, 2016, 8:40:45 PM6/18/16
to Spree
Product must have an option type to assign an option value to its variant.

In your code, you are assigning an option value to your variant but you have not already assigned an option type to your product. Just add a line of option type column while creating products.

I hope this will resolve your issue.

Regards.

omair azam

unread,
Jun 18, 2016, 8:47:35 PM6/18/16
to Spree
You need to make this change. This should work.
 
product_DOJ2034 = Spree::Product.create!(
  
:name => "The Ginnie Ring".titleize,
  
:description => "This graceful ring is sculpted with an entwining band, holding the the precious center stone.",
  
:available_on => Time.now,
  
:price => 680,
  
:sku => "STYLE-DOJ2034",
  
:shipping_category => shipping_category, :tax_category => taxcategory,

  :option_types => [metal]
 
)

Jessa Denmark

unread,
Jun 21, 2016, 7:11:06 PM6/21/16
to Spree
Thank you, that solved the problem
Reply all
Reply to author
Forward
0 new messages