尝试使用python lib创建一个新的Variant。
创建shopify会话后,我只是尝试按照文档
创建Variant实例session = shopify.Session('<My Url>')
session.token = self.provider_access_token
shopify.ShopifyResource.activate_session(session)
v=shopify.Variant(dict(price="20.00", option1="Second"))
v.save()
并获得:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/yoda/.virtualenvs/myproject/src/shopify/shopify/base.py", line 151, in save
return super(ShopifyResource, self).save()
File "/Users/yoda/.virtualenvs/myproject/lib/python2.6/site-packages/pyactiveresource /activeresource.py", line 732, in save
data=self.to_xml())
File "/Users/yoda/.virtualenvs/myproject/lib/python2.6/site-packages/pyactiveresource /connection.py", line 313, in post
return self._open('POST', path, headers=headers, data=data)
File "/Users/yoda/.virtualenvs/myproject/src/shopify/shopify/base.py", line 18, in _open
self.response = super(ShopifyConnection, self)._open(*args, **kwargs)
File "/Users/yoda/.virtualenvs/myproject/lib/python2.6/site-packages/pyactiveresource /connection.py", line 258, in _open
response = Response.from_httpresponse(self._handle_error(err))
File "/Users/yoda/.virtualenvs/myproject/lib/python2.6/site-packages/pyactiveresource /connection.py", line 357, in _handle_error
raise ResourceNotFound(err)
ResourceNotFound: HTTP Error 404: Not Found
有任何想法或提示吗? :)
由于 RCdH
答案 0 :(得分:3)
产品变体必须在列表,计数或创建操作时指定product_id。这可以在Product Variant API docs中看到,因为product_id是URL路径的一部分。出于这个原因,Shopify返回404错误,因为该路径不存在。
Shopify Wiki上的using the shopify_python_api页面有一个关于前缀选项的部分,您可能会觉得有用。
在您的示例中,您只需要更改行
v=shopify.Variant(dict(price="20.00", option1="Second"))
到
v=shopify.Variant(dict(price="20.00", option1="Second", product_id=product.id))
假设您有一个产品可以在变量product
中创建变体。
答案 1 :(得分:0)
您还需要传递变体的ID以及product_id
# get first product
product = shopify.Product.find()[0]
# get product's first variant
variant = product.variants[0]
# pass both the product and variant ids
v=shopify.Variant(dict(price="20.00", product_id=product.id, id=variant.id))
# finally save the variant
v.save()