1
votes

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

1
"@messages={:base=>["Options are not unique"]}" It seems that ActiveResources wants only unique options. Have you looked into that?MrYoshiji
That seems the problem in fact, but Shopify - at least from the web admin - does allow to create a product with 3 different options (size, color,fit) and allows to have 2 different variants both with a small size but with different colors. Does the API have some strictier limitations than the web admin?Augusto

1 Answers

2
votes

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
    )