上传具有多种变体选项的新产品

时间:2015-08-23 07:21:42

标签: ruby shopify

我正在尝试通过ruby上传产品(不使用rails)。我已经通过API上传了100多个产品,但我无法上传具有多个选项值的产品。即使我分配了三个选项值,也不会填充另外两个选项值。

这是脚本:

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,然后对数据进行数组/散列。

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

2 个答案:

答案 0 :(得分:1)

此node.js脚本添加带变体的项目。这里的不同之处在于它包含产品元素的选项列表。请注意,如果您注释掉options元素,那么我会报告您报告的相同问题,即只导入第一个选项。

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

答案 1 :(得分:0)

我使用@bknights代码作为参考,并让我的代码在ruby中工作。您只需要首先在产品级别设置选项名称:

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

然后添加变体工作:

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