是的。这是一个用于向现有产品添加新变体的节点示例。需要注意的是,您必须使用要保留的变体的 id 填充变体数组:
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();