3
votes

Shopify have a RESTful API and a handy shopify_api ruby gem that I already use for updating inventory levels.

Today I have to do something new, creating a new product with image, and I'd like to use the shopify_api gem instead of using something like rest-client or httparty to interact with the API.

The Shopify API supports the simultanious creation of a new product with the default variant and a product image which will be downloaded by Shopify using this call:

POST /admin/products.json

With a JSON request string of:

{
  "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"
      }
    ]
  }
}

The question is how do I do this using the shopify_api gem?

Cheers,

Marcus

1

1 Answers

16
votes

I found an inelegant way to do it. See solution below:

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
)