[Answered ]-Shopify Python API: How do I add a product to a collection?

3👍

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

0👍

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

-1👍

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

Leave a comment