1

在 Rails 3.2.2 中,我尝试使用 Rails Shopify Gem 3.0.1 创建具有多个变体的新 Shopify 产品。

一切都只使用 1 个选项,但是如果我尝试在我的变体中使用 2 个选项,则产品在保存时会出错:

#<ActiveResource::ResourceInvalid: Failed.  Response code = 422.  Response message = Unprocessable Entity.>, @validation_context=nil, @errors=#<ActiveResource::Errors:0x0000010297a5b0 ...>>, @messages={:base=>["Options are not unique"]}> 

这是我的代码:

 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

奇怪的是,如果我删除第 5 个变体(仍然具有“-s-”作为选项 1 的那个),产品会被保存,如果我尝试创建所有 5 个变体,则会出错。

你能给我一些关于我做错了什么的建议吗?

在此先感谢,奥古斯托

4

1 回答 1

2

我忘记在产品中创建选项:

  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
    )
于 2012-12-06T15:13:21.763 回答