Reputation: 1150
In Rails 3.2.2 I'm trying to create a new Shopify product with multiple variants using the Rails Shopify Gem 3.0.1.
Everything works with just 1 option, however if I try to use 2 options in my variants the product gives an error on save:
#<ActiveResource::ResourceInvalid: Failed. Response code = 422. Response message = Unprocessable Entity.>, @validation_context=nil, @errors=#<ActiveResource::Errors:0x0000010297a5b0 ...>>, @messages={:base=>["Options are not unique"]}>
Here is my code:
variants = []
variant = ShopifyAPI::Variant.new(
:option1 => "-s-",
:option2 => "azul",
:option3 => "boxer",
:inventory_management => "shopify",
:inventory_quantity => 350
)
variants << variant
variant = ShopifyAPI::Variant.new(
:option1 => "-m-",
:option2 => "azul",
:option3 => "boxer",
:inventory_management => "shopify",
:inventory_quantity => 495
)
variants << variant
variant = ShopifyAPI::Variant.new(
:option1 => "-l-",
:option2 => "azul",
:option3 => "boxer",
:inventory_management => "shopify",
:inventory_quantity => 543
)
variants << variant
variant = ShopifyAPI::Variant.new(
:option1 => "-xl-",
:option2 => "azul",
:option3 => "boxer",
:inventory_management => "shopify",
:inventory_quantity => 425
)
variants << variant
variant = ShopifyAPI::Variant.new(
:option1 => "-s-",
:option2 => "negro",
:option3 => "boxer",
:inventory_management => "shopify",
:inventory_quantity => 778
)
variants << variant
product = ShopifyAPI::Product.new(
:title => original_p.title,
:product_type => original_p.product_type,
:handle => original_p.handle,
:vendor => original_p.vendor,
:body_html => original_p.body_html,
:template_suffix => original_p.template_suffix,
:tags => original_p.tags,
:variants => variants
)
product.save
The strange thing is that the product get saved if I remove the 5th variant (the one that has still "-s-" as option1), gives error if I try to create all the 5 variants.
Can you please give me some advise on what I am doing wrong?
Thanks in advance, Augusto
Upvotes: 1
Views: 966
Reputation: 1150
I was forgetting to also create the options in the product:
option1 = ShopifyAPI::Option.new(
:name => "first option"
)
options << option1
option2 = ShopifyAPI::Option.new(
:name => "second option"
)
options << option2
option3 = ShopifyAPI::Option.new(
:name => "third option"
)
options << option3
product = ShopifyAPI::Product.new(
:title => original_p.title,
:product_type => original_p.product_type,
:handle => original_p.handle,
:vendor => original_p.vendor,
:body_html => original_p.body_html,
:template_suffix => original_p.template_suffix,
:tags => original_p.tags,
:variants => variants,
:options => options
)
Upvotes: 2