【问题标题】:Shopify Python API Variant Options Don't Write To StoreShopify Python API 变体选项不写入存储
【发布时间】:2017-03-20 22:10:58
【问题描述】:

我似乎无法创建具有多个选项的产品。我已经尝试了所有方法,Shopify 官方库中的文档很差。我查看了整个 API 参考指南并搜索了其他形式,但似乎找不到正确的语法。代码如下。我正在尝试创建具有两个选项的产品,例如 option1 是大小,option2 是颜色。打印输出也没有显示错误消息,但 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"

【问题讨论】:

    标签: python api shopify


    【解决方案1】:

    文档实际上是正确的,但它确实令人困惑。您似乎缺少三个要点:

    • 选项名称设置在产品上,而不是变体上
    • Product.variantsVariant 资源的列表;您需要为所需的每个变体提供Variant 资源
    • 您只需为Variantoption1option2option3 属性中的每一个设置一个字符串

    示例:

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

    请务必注意,每个变体的选项组合必须是唯一的,否则会返回错误。一个未记录的怪癖是,当您没有在父 Product 资源上提供任何 options 时,它会隐含地为您提供一个名为 Style 的选项,同样,如果您不分配任何选项在变体上,它会自动将Default Title 分配给每个变体的option1。由于每个选项组合都是唯一的,如果您不分配任何 optionsoption1 值,那么当您只有一个变体时它不会出错。如果您随后尝试使用多个变体,它会给您带来的错误会令人困惑,因为它指的是变体选项的非唯一性,而不是缺少optionsoption1 参数。

    【讨论】:

    • 谢谢,这个为我省去了很多试错!
    猜你喜欢
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多