【问题标题】:Creating a new product with image using the shopify_api Ruby gem使用 shopify_api Ruby gem 创建带有图像的新产品
【发布时间】:2012-10-12 00:22:00
【问题描述】:

Shopify 有一个 RESTful API 和一个方便的 shopify_api ruby​​ gem,我已经用它们来更新库存水平。

今天我要做一些新的事情,用图像创建一个新产品,我想使用 shopify_api gem 而不是使用 rest-client 或 httparty 之类的东西来与 API 交互。

Shopify API 支持同时创建具有默认变体的新产品和将由 Shopify 使用此调用下载的产品图像:

POST /admin/products.json

使用 JSON 请求字符串:

{
  "product": {
    "title": "Burton Custom Freestlye 151",
    "body_html": "<strong>Good snowboard!</strong>",
    "vendor": "Burton",
    "product_type": "Snowboard",
    "images": [
      {
        "src": "http://example.com/rails_logo.gif"
      }
    ]
  }
}

问题是如何使用 shopify_api gem 做到这一点?

干杯,

马库斯

【问题讨论】:

    标签: ruby shopify


    【解决方案1】:

    我发现了一种不优雅的方法。请参阅下面的解决方案:

    images = []
    image = {}
    image["src"] = "http://i.dailymail.co.uk/i/pix/2012/09/10/article-0-14EE3D4E000005DC-303_634x434.jpg"
    
    images << image
    
    variant = ShopifyAPI::Variant.new(
      :price                => 69.99,
      :inventory_management => 'shopify',
      :inventory_quantity   => 69, 
      :sku => "MS_TEST"
    )
    
    product = ShopifyAPI::Product.new(
    :title => "MS Test",
    :body_html =>  "<strong>Good snowboard!</strong>",
    :vendor => "MS Test",
    :product_type => "MS Test",
    :images => images,
    :variants => variant
    )
    

    【讨论】:

    • 我不得不对此发表评论..我现在要通过这个并且 API 上的 Shopify 文档很荒谬..你必须是一个超级侦探才能理解这件事..例如Shopify 图片字段是一个数组,而不是像其他所有内容一样的字符串。感谢 Marcus 提供了一个很好的解决方案!
    • 如何在变体中链接图片?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    相关资源
    最近更新 更多