Shopify Python API:如何将产品添加到集合中?
我在一个Django应用里使用Shopify的Python API,想要和我的Shopify商店进行互动。
我有一个叫做“热销商品”的集合。
我想对这个集合进行批量更新,也就是添加或移除一些商品。不过,Python API的文档似乎没有详细说明该怎么做。我该如何通过名称获取这个集合?又该如何把商品添加进去呢?
谢谢你的帮助。
这是我找到的内容
x=shopify.CustomCollection.find(handle="best-sellers")
y=shopify.Collect() # 创建一个新的集合
p = shopify.Product.find(118751076) # 获取这个商品
所以问题是,我该如何把上面提到的商品“p”添加到自定义集合“x”里呢?
3 个回答
获取属于某个集合的所有产品
>>> shopify.Product.find(collection_id=841564295)
[product(632910392)]
创建一个新的产品,并且这个产品有多个不同的变种
>>> new_product = shopify.Product()
>>> print new_product.id # Only exists in memory for now
None
>>> new_product.product_type = "Snowboard"
>>> new_product.body_html = "<strong>Good snowboard!</strong>"
>>> new_product.title = "Burton Custom Freestlye 151"
>>> variant1 = shopify.Variant()
>>> variant2 = shopify.Variant(dict(price="20.00", option1="Second")) # attributes can be set at creation
>>> new_product.variants = [variant1, variant2]
>>> new_product.vendor = "Burton"
>>> new_product.save() # Sends request to Shopify
True
>>> new_product.id
1048875193
通过 - http://wiki.shopify.com/Using_the_shopify_python_api#Receive_a_list_of_all_Products
文档看起来不太靠谱,但有一点要记住,那就是实际上应该已经有一个现成的集合被创建好了。
你可以用下面的代码找到它:
collection_id = shopify.CustomCollection.find(handle=<你的_handle>)[0].id
接下来,把找到的 collection_id 和 product_id 加入到一个 Collect 对象中,然后保存。记得先保存你的产品(或者找一个已经存在的产品),然后再保存集合,不然集合就不知道要关联哪个产品了(通过 API),像这样:
new_product = shopify.Product()
new_product.save()
add_collection = shopify.Collect('product_id': new_product.id, 'collection_id': collection_id})
add_collection.save()
还要注意的是,产品和 Collect 之间是一对一的关系。
创建一个收集,用来把产品添加到自定义集合中。
可以通过以下方式使用 Shopify 的 Python API 来实现这个功能:
collect = shopify.Collect({ 'product_id': product_id, 'collection_id': collection_id })
collect.save()