【问题标题】:How to add a product variant with the Ruby gem如何使用 Ruby gem 添加产品变体
【发布时间】:2012-06-11 17:14:03
【问题描述】:

添加产品多属性的正确方法是什么?

我成功创建了产品,它显示在我的 Shopify 后台中。但是价格总是零,数量总是无穷大。

我尝试创建一个设置了 inventory_quantity 和 price 的变体,以及在 prefix_options 中设置了 product_id。

但是管理员总是显示价格为零和数量无穷大。

Rails v3.2.5
shopify_api v3.0.0
shop name: vacation-2

我在执行 API 调用时没有收到错误。我只是在管理员的产品中看不到我的变体数据。

【问题讨论】:

    标签: ruby-on-rails ruby activeresource shopify


    【解决方案1】:

    确保将:inventory_management 属性设置为"shopify",否则数量将不会保持不变。

    我刚刚测试过,效果很好:

    product.variants << ShopifyAPI::Variant.new(
      :option1              => "Large",
      :price                => 12.95,
      :inventory_management => 'shopify',
      :inventory_quantity   => 10
    )
    product.save
    

    【讨论】:

    • 谢谢,这行得通。在 Shopify 的文档中找不到,但我想这就是他们有 wiki 的原因!这是我最终实际使用的:
      shopify_listing = ShopifyAPI::Product.find(listing.shopify_id
      shopify_listing.update_attributes(listing_data)
      variant = ShopifyAPI::Variant.find(shopify_listing.variants.first.id)
      variant.update_attributes({inventory_quantity: listing.quantity, inventory_management: 'shopify', price: listing.price})
    【解决方案2】:

    在不使用产品关联的情况下添加变体:

    ShopifyAPI::Variant.new(
      :product_id           =>  #enter product id,
      :option1              => "Large",
      :price                => 12.95,
      :inventory_management => 'shopify',
      :inventory_quantity   => 10
    )
    product.save
    

    这里的优点是变体对象会使用 API 返回的值进行更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 2020-01-11
      相关资源
      最近更新 更多