Shopify option selection
Shopify has a pre-built JavaScript function that creates separate option selectors from a master selector, hides that master selector and updates it when any option selectors are changed, and returns
You can include this functionality in your element with the following:
After you include the function, you need to address the following:
The master selector
For this JavaScript function to create option selectors, it needs a master selector to read the variants from. This master selector needs to have an id
attribute for the function to reference, as well as an attribute of name=id
to be recognized in the product form as the variant ID source.
For example:
The callback function
The callback function is called when any of the option selectors change, giving you the opportunity to update product elements like the media and price. It can be called anything, but it must have the following two parameters:
For example:
VARIANT
The variant
object passed to the callback function has the following attributes:
Attribute | Description |
| Whether the variant has available inventory. Will be |
| The variant's barcode. |
| The variant's compare price in cents, or |
| The variant's featured image. |
| The variant's ID. |
| The variant's inventory tracking service. |
| Whether the variant should continue selling when the inventory level is 0 or less. Will be |
| The product title combined with the variant title, separated by a |
| The value of the first product option. |
| The value of the second product option, or |
| The value of the third product option, or |
| The variant's price in cents. |
| Whether the variant requires a selling plan. Will be |
| Whether the variant requires shipping. Will be |
| An array of |
| The variant's sku, or |
| Whether the variant is taxable. Will be |
| A combination of the option values with |
| The variant's weight in grams, or |
Your callback should handle the following cases:
The
variant
exists, and is availableThe
variant
exists, and is not availableThe
variant
doesn't exist
SELECTOR
The selector
object passed to the callback function is the calling OptionSelectors
object.
Instantiate the option selection JavaScript
To use the option selection JavaScript, you need to instantiate a new OptionSelectors
object with arguments of the id
attribute of the master selector, and an object with the following attributes:
Attribute | Description |
| The product JSON, which can be accessed using the json filter. |
| The name of your callback function. |
| Determines whether to update the URL with the |
For example:
Last updated