You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is an onClick event that essentially does the following..
onSwatchClick =>
- Deactivate all swatches
- Activate newly selected swatch
- Update main image src
- Update main image href with link to variant
Swatch Appearance
All swatch colors are set using images with a background-color fallback. All swatch images are uploaded using the file uploader in the settings panel. They are all named specifically to match the colors used in the product variants (i.e. purple.png, silver.png).
I'm assuming they just get included in the liquid template in someway similar to this. If the image doesn't exist, nothing gets output and we see the swatch get styled via the background-color.
{% forcolorinall_colors %}
<divclass="swatch"style="background-color: {{ color }}">
{{ color | append:'.png' | file_url | img_tag }}
</div>
{% endfor %}
PDP Swatches
Don't work correctly. Clicking the swatch shows the associated variant image but that image isn't a thumb in the main gallery. Things get a little broken, the hover zoom image doesn't get updated.
The hard coded keys in this case would be "details", "materials", "size-guide", and "why". Splitting the tags on these keys would give you values like "striped-linen" and "duvet-cover" that you can use to pull in snippets or selectively show elements on the page.
PLP Swatches
The general structure for swatches on the PLP looks something like this
<divclass="product-box" data-category="{{ category (i.e. 'striped') }}" data-materials="{{ material (i.e. 'material-striped') }}" data-colors><!-- Images and text up here --><divclass="product-colors"><ul><liclass="option" data-option-titledata-variant-url="{{ url }}" data-variant-img="{{ src }}"><spanclass="color granite-with-ivory material-striped-linen" data-color="granite-with-ivory"></span></li><!-- More swatches here --></ul></div></div>
Check if the product has an option called "Color", loop through all the variants and get all the variants with unique colors (meaning, if you had 3 different colored shirts in S-M-L, you would end up pulling the 3 small shirts in different colors).
Loop through all images and check if they have associated variants that match. Use the variant url and image to build the swatches.
There is an onClick that essentially does the following..
onSwatchClick =>
- Remove selected class from option swatches
- Add selected class to newly selected swatch
- Update main image with variant image url
- Update the main image url to point to the variant
Swatch Appearance
All swatch colors and images are hardcoded into the css. There is a set of rules for styling different swatch colors just using hex color codes and another set of rules for styling all "material" swatches using background images. These are all pngs stored in the theme asset folder. They don't have to figure out a way to do this dynamically because they have a very limited number of products colors and material options. When they want to add a new color or material, they have to add it to the CSS.
All filtering is done on the client, there is no server interaction. This is only possible because of their limited number of products. They only have ~40 products so they'll never have issues with pagination.
Their filtering uses a query parameter called filters which has no server side effect when submitting the URL. The site returns the collection as normal, and then when the filters get initialized, the javascript looks at the url and sets the proper filters accordingly.
There is an onChange event for checkbox that does the following..
onCheckBoxChange =>
- Get the color from the checkbox by pulling off the `color-` prefix
- Use a series of if / else statements to click all swatches that are an exact or close match to the color. This check is hard coded.
- Update the URL using history.replaceState ( ../collections/all??filters={{ material }}+{{ color }} )
- Misc. UI updates
Here's a small piece of what the if / else statements look like. You can see they have to hard code colors whatever colors they want to match the filterable colors.
Loop through each product box, see if it has a class like new-color-{{ color }}. If it does, pop the {{ color }} part off and use that to select the corresponding swatch. We want to show the a new color over an old color automatically.
PDP Swatches and Galleries
PDP has a default gallery with all of the product images included in it. It also has a hidden list of galleries, one for each variant. These galleries are hidden / shown based on the selected swatch.
How does it build these variant galleries? Loop through all the product images and see if the file name matches the variant option value? Somewhat of a mystery..
I did a deep dive into Johnnie-O to see how Magento 2 builds out swatch / gallery relationships (they're using the default M2 color attribute swatch methodology):
Basically, they upload swatch images against a key/value store that goes like so swatch_img | swatch_key | swatch_title. The configuration matches the color attribute with a picker and the image is generated.
It might be doable to replicate the same system in Shopify with metafields, but instead of adding swatch metafields per product, store all available swatches somewhere they can be queried easily. We can store metafields on the 'Shop' object so it's possible to use a metafields plugin to manage..
I did a deep dive into Johnnie-O to see how Magento 2 builds out swatch / gallery relationships (they're using the default M2 color attribute swatch methodology):
Basically, they upload swatch images against a key/value store that goes like so
swatch_img | swatch_key | swatch_title. The configuration matches the color attribute with a picker and the image is generated.It might be doable to replicate the same system in Shopify with metafields, but instead of adding swatch metafields per product, store all available swatches somewhere they can be queried easily. We can store metafields on the 'Shop' object so it's possible to use a metafields plugin to manage..