I have a product uploaded in which I have added a color variant from a metaobject. I have done several tests to show the color but I can’t. Can someone help me solve it .
This is the code I’m testing. It returns the variants but I cannot access the color of the metaobject.
{%- for option in product.options_with_values -%}
{%- if option.name == 'Color' -%}
**{{ option.name }}**:
{%- for value in option.values -%}
{% assign variant = product.variants | where: "option1", value | first %}
{% assign color_metaobject_reference = variant.metafields.shopify.color-pattern %}
{% if color_metaobject_reference %}
{% assign color_metaobject = color_metaobject_reference.value %}
{% if color_metaobject %}
{% assign color_rgb = color_metaobject.color %}
- {{ value }}
{% else %}
- {{ value }}
{% endif %}
{% else %}
- {{ value }}
{% endif %}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
To display variant metafields on the product page in the Shopify Dawn 2.0 theme, follow these steps:
Step 1: At your Shopify Admin, please go to Online Store > Themes> Edit code and Access product-template.liquid File:
Step 2: Look for the code section where product variants are displayed, often in a loop. Ensure that the metafields exist and are named correctly for the code to work.
For example, if your metafields are named “color” you can use the following code:
{{ variant.metafields.global.color }}
Or on your test code
{%- for option in product.options_with_values -%}
{%- if option.name == 'Color' -%}
**{{ option.name }}**:
{%- for value in option.values -%}
{% assign variant = product.variants | where: "option1", value | first %}
{% assign color_metaobject_reference = variant.metafields.global.color %}
{% if color_metaobject_reference %}
{% assign color_metaobject = color_metaobject_reference.value %}
{% if color_metaobject %}
{% assign color_rgb = color_metaobject.color %}
- {{ value }}
{% else %}
- {{ value }}
{% endif %}
{% else %}
- {{ value }}
{% endif %}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
Hope this can help you.
If our suggestion is useful, please let us know by giving a like or mark as a solution. Thank you
Does anyone know how to make code like this work on a localised version of the site?
i.e. when the option name isn’t Color anymore and it becomes something like Couleur.
I know just checking against the localised options for Color would work but was kinda hoping for a way that wouldn’t break when new languages or changes to localisations are made