简体   繁体   中英

Shopify Python API: How do I add a product to a collection?

I am using the Shopify Python API in an django app to interact with my Shopify Store.

I have a collection - called - best sellers.

I am looking to create a batch update to this collection - that is add/remove products to this collection. However, they python API docs does not seem to say much about how to do so. How do I fetch a collection by name? How do I add a product to it?

Thank you for your help.


This is what I found

x=shopify.CustomCollection.find(handle="best-sellers")

y=shopify.Collect() #creates a new collect

p = shopify.Product.find(118751076) # gets me the product

So the questions is how do I add the product "p" above to the Custom Collection "x" ?

Create a collect to add a product to a custom collection.

Shopify API – Collect Documentation

This can be done using the Shopify Python API as follows

collect = shopify.Collect({ 'product_id': product_id, 'collection_id': collection_id })
collect.save()

The documentation is again not promising but one thing to bear in mind is that there should in actual fact be an existing collection already created

Find it by using this code

collection_id = shopify.CustomCollection.find(handle=<your_handle>)[0].id

then consequently add the collection_id, product_id to a Collect object and save, remember to first save your product (or have an existing one which you can find) and then only save your collection, or else the collection won't know what product its posting to (via the api), like so

new_product = shopify.Product()

new_product.save()

add_collection = shopify.Collect('product_id': new_product.id, 'collection_id': collection_id})

add_collection.save()

Also important to note that there is a 1 to 1 relationship between Product and Collect

Fetches all products that belong to a certain collection

>>> shopify.Product.find(collection_id=841564295)
[product(632910392)]

Create a new product with multiple product variants

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

via - http://wiki.shopify.com/Using_the_shopify_python_api#Receive_a_list_of_all_Products

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM