3
votes

I can't seem to create a product with multiple options. I've tried everything and the documentation from Shopify's official library is poor. I've looked all over the API reference guide and search other forms, but can't seem to find the proper syntax. Code is below. I'm trying to create a product with two options, for instance option1 is size and option2 is color. There are no error messages shown too for the printed output, but variant options doesn't appear in the Shopify store, only the product with 0 variants appears.

new_product = shopify.Product()
new_product.title = "My Product"
new_product.handle = "test-product"
##what I've tried... and countless others
#First example of new_product.variants
new_product.variants = shopify.Variant({'options': {'option1' : ['S', 'M', 'L', 'XL'], 'option2' : ['Black', 'Blue', 'Green', 'Red']}, 'product_id': '123456789'})
#Second example of new_product.variants
new_product.variants = shopify.Variant({'options': [{'option1': 'Size', 'option2': 'Colour','option3': 'Material'}]})
#Thrid example of new_product.variants
new_product.variants = shopify.Variant([
                      {'title':'v1', 'option1': 'Red', 'option2': 'M'},
                      {'title':'v2', 'option1' :'Blue', 'option2' :'L'}
                      ])
new_product.save()
##No errors are output, but doesn't create variants with options
if new_product.errors:
    print new_product.errors.full_messages()
print "Done"
1

1 Answers

6
votes

The documentation is actually correct, however it is admittedly confusing. The three main points it seems you're missing:

  • The option names are set on the product, not the variants
  • Product.variants is a list of Variant resources; you need a Variant resource for every variant you want
  • You simply set a string to each of Variant's option1, option2, and option3 attributes

Example:

import shopify

# Authenticate, etc
# ...

new_product = shopify.Product()
new_product.title = "My Product"
new_product.handle = "test-product"
new_product.options = [
    {"name" : "Size"},
    {"name" : "Colour"},
    {"name" : "Material"}
]

colors = ['Black', 'Blue', 'Green', 'Red']
sizes = ['S', 'M', 'L', 'XL']

new_product.variants = []
for color in colors:
    for size in sizes:
        variant = shopify.Variant()
        variant.option1 = size
        variant.option2 = color
        variant.option3 = "100% Cotton"
        new_product.variants.append(variant)

new_product.save()

It's important to note that each variant's option combinations must be unique, or it will return an error. A quirk that isn't documented is that when you don't supply any options on the parent Product resource, it will implicitly give you a single option named Style, and likewise if you don't assign any options on the variant then it will automatically assign Default Title to each variant's option1. Since each option combination is unique, if you don't assign any options or option1 values, then it won't error when you only have a single variant. If you then try with multiple variants, the error it will give you will be confusing, as it will be referring to the non-uniqueness of variant options and not about missing the options and option1 parameters.