1

因此,我使用 Shopify Gem 访问 Shopify API,并注意到 product_id 属性没有在简单的 ShopifyAPI::Variant.find 调用的响应正文中返回。

1.9.3p194> ShopifyAPI::Variant.find(209901733)
 => #<ShopifyAPI::Variant:0x007fbf7225d3f0 @attributes={"barcode"=>nil, "compare_at_price"=>"198.00", "created_at"=>"2012-03-23T14:11:39+11:00", "fulfillment_service"=>"manual", "grams"=>1000, "id"=>209901733, "inventory_management"=>"shopify", "inventory_policy"=>"deny", "option1"=>"38", "option2"=>"Ivory Mini Twill", "option3"=>nil, "position"=>16, "price"=>"198.00", "requires_shipping"=>true, "sku"=>"3063", "taxable"=>true, "title"=>"38 / Ivory Mini Twill", "updated_at"=>"2013-04-24T10:25:27+10:00", "inventory_quantity"=>2}, @prefix_options={}, @persisted=true> 

根据此处发布的新文档,应该返回 product_id 字段。

GET /admin/variants/#{id}.json
Hide Response
HTTP/1.1 200 OK

{
  "variant": {
    "barcode": "1234_pink",
    "compare_at_price": null,
    "created_at": "2013-05-01T15:35:21-04:00",
    "fulfillment_service": "manual",
    "grams": 200,
    "id": 808950810,
    "inventory_management": "shopify",
    "inventory_policy": "continue",
    "option1": "Pink",
    "option2": null,
    "option3": null,
    "position": 1,
    "price": "199.00",
    "product_id": 632910392,
    "requires_shipping": true,
    "sku": "IPOD2008PINK",
    "taxable": true,
    "title": "Pink",
    "updated_at": "2013-05-01T15:35:21-04:00",
    "inventory_quantity": 10
  }
}
4

2 回答 2

2

它在 json 中,但不在从 json 创建的 ActiveResource 中。原因是 Variant activeresource 中的这段代码:

    self.prefix = "/admin/products/:product_id/"

    def self.prefix(options={})
      options[:product_id].nil? ? "/admin/" : "/admin/products/#{options[:product_id]}/"
    end

如果您愿意,可以创建自己的类来获取单例变体:

      module ShopifyAPI
        class VariantWithProduct < Base
          self.prefix = "/admin/"
          self.element_name = "variant"
          self.collection_name = "variants"
        end
      end

并使用此类按 id 获取单个变体:

    ShopifyAPI::VariantWithProduct.find(xxxxxx)
于 2013-05-23T06:57:57.247 回答
2

迈克尔对问题的诊断是正确的。对我来说,解决这个问题的最简单方法是获取产品资源而不是变体。ShopifyAPI::ProductActiveResource 对象确实包含变体。

product = ShopifyAPI::Product.find(product_id)
variant = product.variants.find { |v| v.id == variant_id }
于 2013-06-14T19:02:38.057 回答