【问题标题】:Creating a new product with image URL in shopify using its python api使用其 python api 在 shopify 中创建具有图像 URL 的新产品
【发布时间】:2012-11-01 02:24:11
【问题描述】:

我希望从现有的网络应用程序自动将商品发布到 shopify 商店。我需要能够创建带有图像的项目才能这样做。我已经能够通过 python api 在 shopify 上创建项目 - 但不确定如何添加图像。这是我现在拥有的:

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()

如何在其中添加图像?

new_product.images 似乎不起作用。

非常感谢任何和所有帮助。 :-)

谢谢。

【问题讨论】:

    标签: python shopify


    【解决方案1】:

    我想通了。 :-)

        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()
    

    【讨论】:

    • 如何添加多于 1 张图片。我很难弄清楚。谢谢你的帮助
    【解决方案2】:

    我知道问题已经得到解答,但这里也有另一种方法。

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

    例如,如果您已经将产品 ID 保存在数据库中,那么您可以使用此路由并为自己保存对 API 的调用。

    【讨论】:

    • src 必须是 http url 还是本地文件?基本上,它会像图片上传一样工作吗?
    • 否,但它可以作为 base64 编码图像在正文中传递。查看文档:docs.shopify.com/api/product_image#create
    • 是的,感谢您的回复。我查看了源代码,发现这个函数 Image.def attach_image(data, filename=None) 完成了你刚才提到的。效果很好。我昨晚上传了照片。
    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多