【问题标题】:Uploading new products with multiple variant options上传具有多个变体选项的新产品
【发布时间】:2015-08-23 07:21:41
【问题描述】:

我正在尝试通过 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 所需的知识,但如果你能稍微帮助我,我将不胜感激。

【问题讨论】:

  • 我也在为此苦苦挣扎。你找到解决办法了吗?
  • 我找到了答案,你只需要先在产品上设置选项。我在答案中添加了我的代码。

标签: ruby shopify


【解决方案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();

【讨论】:

    【解决方案2】:

    我使用@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
    

    【讨论】:

      猜你喜欢
      • 2014-05-27
      • 2020-06-21
      • 1970-01-01
      • 2020-09-05
      • 2013-10-20
      • 1970-01-01
      • 2020-07-14
      • 2017-06-09
      • 2014-11-05
      相关资源
      最近更新 更多