2017-03-20 8 views
2

複数のオプションを持つ製品を作成することはできません。私はすべてを試してきましたが、Shopifyの公的図書館の文書は貧弱です。私はAPIリファレンスガイド全体を見て、他のフォームを検索しましたが、適切な構文が見つからないようです。コードは以下のとおりです。私は2つのオプションを持つ製品を作成しようとしています。たとえば、option1はサイズで、option2はカラーです。印刷出力にもエラーメッセージは表示されませんが、バリアントオプションはShopifyストアには表示されず、バリアントが0の製品のみが表示されます。Shopify Python APIバリアントオプションストアに書き込むことはありません

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" 

答えて

1

ドキュメントは実際には正しいですが、それは間違いありません。 3つの主なポイントそれはあなたが不足しているようだ:

  • オプション名は、製品に設定されている、いない変種
  • Product.variantsVariantリソースのリストがあります。あなたはあなたが単にVariantoption1のそれぞれに文字列を設定し
  • をしたいすべてのバリアント、option2ためVariantのリソースを必要とし、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() 

を各バリアントのオプションの組み合わせが一意でなければならないこと、またはエラーを返すことに注意してください。あなたがProductという親リソースにoptionsを指定しないと暗黙のうちにStyleという単一のオプションが与えられ、同様にそのバリアントにオプションを割り当てないと、文書化されていないことがあります。各バリアントのoption1に自動的にDefault Titleを割り当てます。各オプションの組み合わせは一意であるため、optionsまたはoption1の値を割り当てないと、1つのバリアントしか持たない場合でもエラーになりません。複数のバリアントを試してみると、バリアントオプションの非一意性を指すようになり、optionsoption1のパラメータが欠落していないため、エラーが混乱します。

関連する問題