[英]Python Shopify API add 2 pictures to new product
I am trying to add 2 pictures to my new shopify store with the api. 我正在尝试使用api将2张图片添加到我的新shopify商店中。 However, I need to upload 2 pictures not just one.
但是,我需要上传2张图片,而不仅仅是一张。 this is what I have so far but its not working please advise.
这是我到目前为止的内容,但无法正常工作,请告知。
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()
This code will only upload one image to the Product as you are passing the same encoded value for both the images. 当您为两个图像传递相同的编码值时,此代码只会将一个图像上传到产品。 I have edited the code below and separated creating Image objects for both the images.
我已经编辑了下面的代码,并分别为两个图像创建了Image对象。 This will upload both the images to Shopify.
这会将两个图像都上传到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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.