Paul C
Paul C

Reputation: 1

Shopify - Product Variant API

Is there a way to create multiple product variants for single product with one POST call?

I know a single product variant can be made with:

POST /admin/products/#{id}/variants.json
{
   "variant": 
   {
      "option1": "Default Title",
      "price": "1.00"
    }
}

Is it possible to execute a single POST to create multiple variants for the same product id?

Upvotes: 0

Views: 2883

Answers (1)

bknights
bknights

Reputation: 15402

Yes. Here is a node sample for adding new variants to an existing product. The caveat is that you have to populate the variants array with the ids of the variants you want to keep:

var https = require('https');

var cred = new Buffer("xxx:yyy").toString('base64');

var headers = {Authorization: "Basic "+cred, "Content-Type": "application/json"};
var productId = 1925263361;
var options = {
  host: 'kotntest1.myshopify.com',
  port: 443,
  path: '/admin/products/'+productId +'.json',
  method: 'PUT',
  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);
    console.dir(resultObject);
  });
});

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

var product = {
  product:{
    id: productId, 
    variants: [
      {
        id:5991257025 //existing variant id
      },
      {
        id:5991257089 //existing variant id
      },
      {
        id:19762423495 //existing variant id
      },
      // new variant details
      {
        title:'v4', // new variant details
        option1: 'green',
        option2: "Honda", 
        option3: 'Civic'
      },{
        title:'v5',
        option1: 'pink',
        option2: "Honda", 
        option3: 'Civic'
      },{
        title:'v6',
        option1: 'yellow',
        option2: "Honda", 
        option3: 'Civic'
      },{
        title:'v7',
        option1: 'brown',
        option2: "Honda", 
        option3: 'Civic'
      }
      ]
    }

};

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

Upvotes: 0

Related Questions