Hi, im trying to access this hex code which is using category metafields created by Shopify to create swatches on the collections page. How do i access this? Im trying numerous liquid syntax. I am using Dawn theme.
To display swatches for your products on the collections page, you need to properly access and render the metafield data from Shopify. Based on the code you’ve shared and the metafield setup visible in your images, I’ll guide you through troubleshooting and refining the Liquid code.
Refining the Code Below is the corrected and optimized Liquid code snippet to achieve swatches:
{% assign keys = "Metal_Type,Metal Type" | split: ',' %}
{% for key in keys %}
{% if card_product.options contains key %}
{% for color_option in card_product.options_by_name[key].values %}
{% assign background_style = '' %}
{% for variant in card_product.variants %}
{% if variant.options contains color_option %}
{% assign color_metafield = variant.metafields['color-pattern'] %}
{% if color_metafield and color_metafield.color %}
{% assign background_style = color_metafield.color.value %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
CSS for Swatches:
Add relevant CSS to style the swatches. For example:
Let me know if you encounter issues or need further clarification!
If I was able to help you, please don’t forget to Like and mark it as the Solution!
If you’re looking for expert help with customization or coding, I’d be delighted to support you. Please don’t hesitate to reach out via the email in my signature below—I’m here to help bring your vision to life!