Daniel Ryan Snell
Daniel Ryan Snell

Reputation: 119

Uploading new products with multiple variant options

I am trying to upload products via ruby (not with rails). I have uploaded 100 + products via API, although I cannot upload a product with more than one option value. Even if I assign three option values, it will not populate the other two.

Here is the script:

require 'shopify_api' 
require 'open-uri' 
require 'json'

begin_time = Time.now

shop_url  = "*https*(yes I know the * are their)://-YouWish-:-I'[email protected]/admin/products.json"
include ShopifyAPI

ShopifyAPI::Base.site ="*https*://-YouWish-:-I'[email protected]/admin/"

raw_product_data = JSON.parse(open('omg.json') {|f| f.read }.force_encoding('UTF-8'))
raw_product_data_size = raw_product_data.size

puts '========================================================================='
puts "#{raw_product_data_size} seconds till explosion. assistance
needed..."
puts '-------------------------------------------------------------------------'
single_product_begin_time = Time.now

# Create new product   
new_product = ShopifyAPI::Product.new  
new_product.title = "Variants Suck"
new_product.body_html = "So"  
new_product.product_type = "Much"
new_product.vendor = "Please"  
new_product.tags = "Help"

new_product.variants = [ 
     {
      "option1" => "This One Works",
      "option2" => "Lost Cause",
      "option3" => "/wrist",
      "postion" => "1",
      "price" => "10.00",
      "sku" => "12345",
      "inventory_management" => "shopify",
    }   ]
 new_product.images = [
    {
      src: "https://cdn.shopify.com/s/files/1/0750/0067/files/Pro-Tapes.jpg?11603036243532110652"
    }   ]

  new_product.save

  creation_time = Time.now - single_product_begin_time


puts '-------------------------------------------------------------------------'
puts "Sorry About the mess babe, atleast it only took #{begin_time - Time.now} minutes."
puts '========================================================================='

I am testing this on a dev shop, but I am attempting to rebuild something previously built on magento, where I can have people convert my csv data entry to json, then array/hash the data.

Please don't link me to the (shopify)/API info. I have read it. I don't understand the formatting of it. If I were to shopify-cli console, and paste the api example in irb, it won't execute properly. I am sure I am just lacking the required knowledge of working with APIs, although if you can help me just slightly it would be much appreciated.

Upvotes: 3

Views: 2075

Answers (2)

amorimluc
amorimluc

Reputation: 1719

I used @bknights code as a reference and got my code to work in ruby. You just have to set the option names on the product level first:

new_product = ShopifyAPI::Product.new
new_product.options = [{"name" => "Size"}, {"name" => "Color"}]

Then adding variants work:

new_product.variants = [ 
    {
      "option1" => "S",
      "option2" => "Black",
      "position" => "1",
      "price" => "10.00"
    },
    {
      "option1" => "M",
      "option2" => "Black",
      "position" => "1",
      "price" => "10.00"
    }
]
new_product.save

Upvotes: 0

bknights
bknights

Reputation: 15367

This node.js script adds item with variants. The difference here is that it includes a list of options on the product element. Note that if you comment out the options element then I get the same problem you are reporting in that only the first option is imported.

var https = require('https');

var cred = new Buffer(privateAppAPIKey +":"+ privateAppPassword).toString('base64');

var headers = {Authorization: "Basic "+cred, "Content-Type": "application/json"};

var options = {
  host: 'kotntest1.myshopify.com',
  port: 443,
  path: '/admin/products.json',
  method: 'POST',
  headers: headers
};

// Setup the request.  The options parameter is
// the object we defined above.
var req = https.request(options, function(res) {
  res.setEncoding('utf-8');

  var responseString = '';

  res.on('data', function(data) {
    responseString += data;
    console.log(data);
  });

  res.on('end', function() {
    var resultObject = JSON.parse(responseString);
  });
});

req.on('error', function(e) {
  // TODO: handle error.
  console.log(e);
});

var product = {
  product:{
    title:'My First Test Product',
    options : [
      {name : "First"},
      {name : "Second"},
      {name : "Third"}
    ],   
    variants: [
      {
        title:'v1',
        option1: 'Red',
        option2: "Honda", 
        option3: 'Prelude'
      },
      {
          title:'v2',
          option1 :'Blue',
          option2 :'Ford',
          option3 :'Escort'
      }
      ]
    }

};

req.write(JSON.stringify(product));
req.end();

Upvotes: 1

Related Questions