Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shopify API Python Multiple Pictures upload with Python API

I am trying to add more than one picture on Shopify with the Python API however, I am not able to upload 2 pictures to one product. At this time only one picture is being uploaded. How I can add more than 1 picture to Shopify API?

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

shop_url = "https://%s:%[email protected]/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()
like image 250
ben olsen Avatar asked May 23 '18 11:05

ben olsen


1 Answers

I don't have shopify account to verify the below code, but I have looked at the source code and below is what should work for you

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

shop_url = "https://%s:%[email protected]/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]
success = new_product.save()

if success:
    product_id = new_product.id
    image = shopify.Image({"product_id": product_id})
    image2 = shopify.Image({"product_id": product_id})
    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)
    image.save()
    image2.save()

The idea is create the product and then attach the image

like image 99
Tarun Lalwani Avatar answered Sep 27 '22 19:09

Tarun Lalwani