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 番目のバリアント (オプション 1 として「-s-」がまだ残っているもの) を削除すると製品が保存され、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 に答える