Python Shopify API将2张图片添加到新产品中

时间:2018-05-24 23:36:46

标签: python shopify

我正在尝试使用api将2张图片添加到我的新shopify商店。但是,我需要上传2张图片而不仅仅是一张。这是我到目前为止,但它不起作用请告知。

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"
path2 = "audi2.jpg"

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


variant = shopify.Variant({'price': 1.00, 'requires_shipping': False,'sku':'000007'})
new_product.variants = [variant]
image = shopify.Image()
image2 = shopify.Image()



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

new_product.images = [image,image2]
new_product.save()

1 个答案:

答案 0 :(得分:0)

当您为两个图像传递相同的编码值时,此代码只会将一个图像上传到产品。我已经编辑了下面的代码,并分别为两个图像创建了Image对象。这会将两个图像都上传到Shopify。

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

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