I'll walk you through some examples, starting with translating the Product resource examples from the API documentation (
http://api.shopify.com/product.html). I'll show you as an example interactive session using the data from the documentation, similarly you could use shopify_api.py script (which works .
# Receive a list of all Products:
# Get all products
>>> products = shopify.Product.find()
>>> print products
[product(632910392), product(921728736)]
# You can access attributes directly:
>>> print products[0].handle
ipod-nano
# But for debugging/learning you might want to get a hash of all the attributes:
>>> print products[0].attributes
{'template_suffix': None, 'handle': 'ipod-nano', 'product_type': 'Cult Products', 'tags': 'Emotive, Flash Memory, MP3, Music', 'created_at': datetime.datetime(2011, 9, 6, 11, 46, 13, tzinfo=tzoffset(None, -14400)), 'title': 'IPod Nano - 8GB', 'body_html': '<p>It's the small iPod with one very big idea: Video. Now the world's most popular music player, available in 4GB and 8GB models, lets you enjoy TV shows, movies, video podcasts, and more. The larger, brighter display means amazing picture quality. In six eye-catching colors, iPod nano is stunning all around. And with models starting at just $149, little speaks volumes.</p>', 'updated_at': datetime.datetime(2011, 9, 6, 11, 46, 13, tzinfo=tzoffset(None, -14400)), 'published_at': datetime.datetime(2007, 12, 31, 19, 0, tzinfo=tzoffset(None, -18000)), 'id': 632910392, 'images': [image(850703190)], 'vendor': 'Burton', 'variants': [variant(808950810), variant(49148385), variant(39072856), variant(457924702)], 'options': [option(None)]}
# You can access sub-resources (e.g. options, images and variants for Products) the same way
>>> print products[0].images[0].src
>>> print products[0].options[0].attributes
{'name': 'Title'}
# Fetches all products that belong to a certain collection
>>> print shopify.Product.find(collection_id= 841564295)
[product(632910392)]
# Get all products after the specified ID
>>> print shopify.Product.find(since_id=632910392)
[product(921728736)]
# Get all products, showing only some attributes
>>> products = shopify.Product.find(fields='id')
>>> print products
[product(632910392), product(921728736)]
>>> print products[0].attributes
{'images': [image(850703190)], 'id': 632910392, 'title': 'IPod Nano - 8GB'}
# Receive a count of all Products:
# Count all products
>>> print shopify.Product.count()
2
# Count all products that belong to a certain collection
>>> print shopify.Product.count(dict(collection_id=850703190))
1
# Receive a single Product:
# Get a single product by ID
>>> print shopify.Product.find(632910392)
product(632910392)
# Get only particular fields
>>> product = shopify.Product.find(632910392)
>>> print product.attributes.keys()
['images', 'id', 'title']
# Create a new Product
# Create a new product with multiple product variants
>>> new_product = shopify.Product()
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"
>>> print new_product.save() # Sends request to Shopify
True
1048875193
# Create a new product with the default product variant (using create)
>>> product = shopify.Product.create(dict(product_type="Snowboard", body_html="<strong>Good snowboard!</strong>", title="Burton Custom Freestlye 151", tags="Barnes & Noble, John's Fav, \"Big Air\"", vendor="Burton"))
>>> print
product.id # The create method already called save
1048875194
# Create a new, but unpublished product (using constructor for setting attributes)
>>> product = shopify.Product(dict(product_type="Snowboard", body_html="<strong>Good snowboard!</strong>", title="Burton Custom Freestlye 151", published=False, vendor="Burton"))
>>> product.save()
# Modify an existing Product
# Hide a published product by changing the published attribute to false
>>> shopify.Product(dict(id=632910392, published=False)).save()
# Show a hidden product by changing the published attribute to true
>>> shopify.Product(dict(id=632910392, published=True)).save()
# Update a product's tags (use existing product object)
>>> product = shopify.Product.find(632910392)
>>> product.tags = "Barnes & Noble, John's Fav"
>>> product.save()
# Remove a Product from the database
# Using existing object
>>> product = shopify.Product.find(632910392)
>>> product.destroy()
# Avoiding find where id is known
>>> product = shopify.Product(dict(id=632910392))
>>> product.destroy()
The other tricky thing that I will walk you through is prefix options, which you can think of as required arguments since they are used as part of the URL (as seen in the API docs). For example, the resource Article has a prefix option for the blog id, which is used directly in the URL. So, shopify.Article.find() doesn't, instead call shopify.Article.find(blog_id=241253187).
# Examples for the Article resource
>>> shopify.Article.find(blog_id=241253187)
>>> shopify.Article.count(dict(blog_id=241253187))
>>> shopify.Article.find(134645308 , blog_id=241253187)
>>> article = shopify.Article(prefix_options=dict(blog_id=241253187))
>>> article.title = "My new Article title"
>>> article.save() # create
>>> article.author = "John Smith"
>>> article.save() # update
>>> article.destroy()
When an resource is saved (for creating or updating), validation errors may occur. The error messages can be retrieved as follows:
>>> article = shopify.Article(prefix_options=dict(blog_id=241253187))
>>> article.title = ""
>>> print article.save()
False
>>> print article.errors.full_messages()
["title can't be blank"]
I tried to make this extensive, since I plan on creating a page on the wiki to help others with the same problem. Please let me know if anything is not clear enough.