I’m working on a project where I need to upload several photos for each product using Shopify’s Python library. My current setup only manages to add one image per product, even though I’m trying to attach multiple files. I’ve been struggling with this for a while and can’t figure out what’s going wrong in my approach.
import shopify
API_TOKEN = 'your_api_token_here'
SECRET_KEY = 'your_secret_key_here'
store_url = "https://%s:%[email protected]/admin" % (API_TOKEN, SECRET_KEY)
shopify.ShopifyResource.set_site(store_url)
first_image = "car1.png"
second_image = "car2.png"
product = shopify.Product()
product.title = "Car Photo Collection"
product.body_html = "Multiple car images for testing purposes"
product_variant = shopify.Variant({'price': 25.99, 'requires_shipping': True, 'sku': 'CAR001'})
product.variants = [product_variant]
img1 = shopify.Image()
img2 = shopify.Image()
with open(first_image, "rb") as file:
file_name = first_image.split("/")[-1:][0]
file_name2 = second_image.split("/")[-1:][0]
image_data = file.read()
img1.attach_image(image_data, filename=file_name)
img2.attach_image(image_data, filename=file_name2)
product.images = [img1, img2]
product.save()
What am I missing to make both images upload correctly?