Shopify Python API变体选项不写入存储

我似乎无法创建具有多个选项的产品.我已经尝试了一切,Shopify官方库中的文档很差.我遍历了API参考指南并搜索了其他形式,但是似乎找不到合适的语法.代码如下.我正在尝试创建具有两个选项的产品,例如option1是size和option2是color.对于打印输出,也没有显示错误消息,但是Shopify商店中没有显示变体选项,仅显示具有0个变体的产品.

new_product = shopify.Product()
new_product.title = "My Product"
new_product.handle = "test-product"
##what I've tried... and countless others
#First example of new_product.variants
new_product.variants = shopify.Variant({'options': {'option1' : ['S', 'M', 'L', 'XL'], 'option2' : ['Black', 'Blue', 'Green', 'Red']}, 'product_id': '123456789'})
#Second example of new_product.variants
new_product.variants = shopify.Variant({'options': [{'option1': 'Size', 'option2': 'Colour','option3': 'Material'}]})
#Thrid example of new_product.variants
new_product.variants = shopify.Variant([
                      {'title':'v1', 'option1': 'Red', 'option2': 'M'},
                      {'title':'v2', 'option1' :'Blue', 'option2' :'L'}
                      ])
new_product.save()
##No errors are output, but doesn't create variants with options
if new_product.errors:
    print new_product.errors.full_messages()
print "Done"

解决方法:

该文档实际上是正确的,但是无疑会造成混淆.似乎您缺少的三个要点:

>选项名称是在产品上设置的,不是型号
> Product.variants是Variant资源的列表;您需要每个想要的变体的变体资源
>您只需为Variant的option1,option2和option3每个属性设置一个字符串

例:

import shopify

# Authenticate, etc
# ...

new_product = shopify.Product()
new_product.title = "My Product"
new_product.handle = "test-product"
new_product.options = [
    {"name" : "Size"},
    {"name" : "Colour"},
    {"name" : "Material"}
]

colors = ['Black', 'Blue', 'Green', 'Red']
sizes = ['S', 'M', 'L', 'XL']

new_product.variants = []
for color in colors:
    for size in sizes:
        variant = shopify.Variant()
        variant.option1 = size
        variant.option2 = color
        variant.option3 = "100% Cotton"
        new_product.variants.append(variant)

new_product.save()

重要的是要注意,每个变体的选项组合必须唯一,否则将返回错误.一个没有记录的怪癖是,当您在父产品资源上不提供任何选项时,它将隐式地给您一个名为Style的选项,同样,如果您没有在变体上分配任何选项,那么它将自动为每个变体的选项分配默认标题1.由于每个选项组合都是唯一的,因此,如果您不分配任何选项或option1值,则只有一个变量时不会出错.如果您随后尝试使用多个变体,它将给您带来的错误将使您感到困惑,因为它将指的是变体选项的非唯一性,而不是缺少选项和option1参数.

上一篇:我需要使用Saxon java工具(命令行)从xsl查询mysql数据库?


下一篇:c# – cdata-section-elements不工作