Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new product with image URL in shopify using its python api

Tags:

python

shopify

I am looking to automatically publish items to a shopify store from an existing web-app. I need to be able to create items with images to do so. I have been able to create items on shopify via the python api - but am not sure on how to add the images. Here is what I have right now:

all_products = Product.objects.all()[0:7]
for p in all_products:
    images=[]
    image={}
    image["src"] = p.image.url

    new_product = shopify.Product()
    new_product.product_type = p.category()
    new_product.body_html = p.description
    new_product.title = p.caption
    new_product.vendor = "atisundar"
    new_product.images = images
    new_product.save()

How do I add images to this?

new_product.images does not seem to work.

Any and all help is greatly appreciated. :-)

Thank you.

like image 524
Mayank R Jain Avatar asked Nov 01 '12 02:11

Mayank R Jain


2 Answers

I figured it out. :-)

    new_product = shopify.Product()
    new_product.product_type = p.category()
    new_product.body_html = p.description
    new_product.title = "atisundar "+ p.caption
    new_product.vendor = "atisundar"

    image1 = shopify.Image()
    image1.src = p.image.url()

    new_product.images = [image1]
    new_product.save()
like image 108
Mayank R Jain Avatar answered Sep 28 '22 03:09

Mayank R Jain


I know the question has already been answered but there is an alternate method here as well.

new_image = shopify.Image(dict(product_id=product.id))
new_image.src = "http://someurlhere"
new_image.save()

If you already have the product ID saved in a database, for example, then you can use this route and save yourself a call to the API.

like image 28
ChrisC Avatar answered Sep 28 '22 03:09

ChrisC