【问题标题】:Shopify - Product Variant APIShopify - 产品变体 API
【发布时间】:2016-08-13 20:09:33
【问题描述】:

有没有一种方法可以通过一次 POST 调用为单个产品创建多个产品变体?

我知道可以使用以下方式制作单个产品变体:

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

是否可以执行单个 POST 来为同一个产品 ID 创建多个变体?

【问题讨论】:

    标签: shopify variant


    【解决方案1】:

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

    【讨论】:

    • 感谢 bk 的回复,但我的问题是产品本身已经存在。我希望为现有产品添加新的变体,所以不要认为产品 API 上的 POST 会起作用。我工作的行业是服装,设计师不断为现有产品添加颜色和/或尺寸并不罕见。这就是为什么在我的示例中我使用调用 Product Variant API。
    • 只需使用 PUT。查看我的新示例
    猜你喜欢
    • 2013-01-06
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 1970-01-01
    • 2020-09-05
    • 2016-06-11
    相关资源
    最近更新 更多