使用Shopify API Python,添加带有价格的新产品,并且' requires_shipping':False

时间:2018-02-01 16:37:40

标签: python api shopify variants

我正在尝试通过Python Shopify API添加新产品。我知道如何添加标题和正文以及图片,但是我在添加价格方面存在问题,我需要提出' requires_shipping':False。我无法在任何地方找到如何实现这一目标。

这是我到目前为止所做的。

import shopify    
API_KEY = 'dsfsdsdsdsdsad'
PASSWORD = 'sadsdasdasdas'

shop_url = "https://%s:%s@teststore.myshopify.com/admin" % (API_KEY, PASSWORD)
shopify.ShopifyResource.set_site(shop_url)



path = "audi.jpg"

new_product = shopify.Product()
new_product.title = "Audi pictures test "
new_product.body_html = "body of the page <br/><br/> test <br/> test"

###########this part is so far good. but the bottom part is not working#### 

variant = shopify.Variant(price=9.99)) # this does not work
new_product.variant() # this does not work
variant_2 = shopify.Variant(requires_shipping=False) #this does not work
new_product.variant_2() This does not work 



image = shopify.Image()

with open(path, "rb") as f:
    filename = path.split("/")[-1:][0]
    encoded = f.read()
    image.attach_image(encoded, filename=filename)

new_product.images = [image] # Here's the change
new_product.save()

1 个答案:

答案 0 :(得分:2)

只有前缀选项(例如,变体的product_id,Fulfillments的order_id)才应作为显式参数传递给构造函数。如果要初始化资源的属性,则需要将其作为dict传递。

您也无法随时将新版本与新产品相关联。

这应该有所帮助:

new_product = shopify.Product()
new_product.title = "Shopify Logo T-Shirt"
new_product.body_html = "<b>Test description</b>"
variant = shopify.Variant({'price': 9.99, 'requires_shipping': False})
new_product.variants = [variant]
new_product.save()
=> True

您也可以在初始化后指定资源的属性,就像您已经为产品资源所做的那样。

variant = shopify.Variant()
variant.price = 9.99
variant.requires_shipping = False

另一种选择是首先保存产品并通过明确传递product_id来初始化变体,例如

shopify.Variant(product_id=1234567)

请查看README了解更多用法示例。