如何使用单个API调用创建包含元数据的产品?

时间:2014-10-09 09:38:32

标签: ruby shopify

对于Shopify商店,我想使用metafield次呼叫创建API的产品。

以下是我正在尝试的内容:

require 'shopify_api'

ShopifyAPI::Base.site =  "https://<REDACTED>:<REDACTED>@<REDACTED>.myshopify.com/admin";

new_product = ShopifyAPI::Product.new
new_product.title = "Burton Custom Freestlye 151"
new_product.product_type = "Snowboard"
new_product.vendor = "Burton"

new_product.variants = [
  {
    "option_1" => "First",
    "price" => 100,
    "sku" => 'test123',
    "metafields" => [
      {
        "key" => "item_size",
        "value" => '125gr',
        "value_type" => "string",
        "namespace" => "global"
      }
    ]
  }
]

new_product.save

new_product.metafields
# => #<ActiveResource::Collection:0x007f8de3bf9820
 @elements=[],
 @original_params={},
 @resource_class=ShopifyAPI::Metafield>

但这不起作用。

我知道我可以做到以下几点:

require 'shopify_api'

ShopifyAPI::Base.site =  "https://<REDACTED>:<REDACTED>@<REDACTED>.myshopify.com/admin"

new_product = ShopifyAPI::Product.new
new_product.title = "Burton Custom Freestlye 151"
new_product.product_type = "Snowboard"
new_product.vendor = "Burton"

new_product.variants = [
  {
    "option_1" => "First",
    "price" => 100,
    "sku" => 'test123'
  }
]

new_product.save

new_product.add_metafield(ShopifyAPI::Metafield.new(:namespace => "global", :key => "item_size", :value => "125gr", :value_type => "string"))

new_product.metafields

但是由于我必须导入~26000产品,并且API调用限制为每秒2,我需要尽可能高效。

我接下来可以尝试什么?

1 个答案:

答案 0 :(得分:0)

Shopify员工Shayne pointed out

  

在该示例中,您似乎将变量放在变量上,然后在最后您查看产品以查看是否存在任何元变量。产品和变体都可以有metafields,但是如果你将metafield放在变体上,那就是它将保留的位置。

以下是如何使用产品元字段创建产品:

require 'shopify_api'

ShopifyAPI::Base.site =  "https://<REDACTED>:<REDACTED>@<REDACTED>.myshopify.com/admin"

new_product = ShopifyAPI::Product.new
new_product.title = "Burton Custom Freestlye 151"
new_product.product_type = "Snowboard"
new_product.vendor = "Burton"

new_product.variants = [
  {
    "option_1" => "First",
    "price" => 100,
    "sku" => 'test123'
  }
]

new_product.metafields = [
  {
    "key" => "item_size",
    "value" => '125gr',
    "value_type" => "string",
    "namespace" => "global"
  }
]

new_product.save