上传具有多种变体选项的新产品 [英] Uploading new products with multiple variant options

查看:55
本文介绍了上传具有多种变体选项的新产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 ruby​​(而不是 Rails)上传产品.我已经通过 API 上传了 100 多个产品,但我无法上传具有多个选项值的产品.即使我分配了三个选项值,它也不会填充其他两个.

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.

脚本如下:

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

begin_time = Time.now

shop_url  = "*https*(yes I know the * are their)://-YouWish-:-I'dShareNakedPics-@dev-tactical.myshopify.com/admin/products.json"
include ShopifyAPI

ShopifyAPI::Base.site ="*https*://-YouWish-:-I'dShareNakedPics-@dev-tactical.myshopify.com/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 '========================================================================='

我正在开发商店对此进行测试,但我正在尝试重建以前在 magento 上构建的东西,在那里我可以让人们将我的 csv 数据条目转换为 json,然后对数据进行数组/散列.

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.

请不要将我链接到 (shopify)/API 信息.我已经读过了.我不明白它的格式.如果我要使用 shopify-cli 控制台,并将 api 示例粘贴到 irb 中,它将无法正确执行.我确信我只是缺乏使用 API 所需的知识,但如果您能稍微帮助我,我将不胜感激.

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.

推荐答案

这个 node.js 脚本添加了带有变体的项目.这里的区别在于它包含产品元素上的选项列表.请注意,如果您注释掉 options 元素,那么我会遇到您报告的相同问题,即仅导入了第一个选项.

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();

这篇关于上传具有多种变体选项的新产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆