Ad

Product Variant Do Not Have Metafields In Theme

- 1 answer

I am trying to figure out on how to get product variant metafields.

Here is the code which i use to test it. I have put the code into product.liquid theme file

<script type="text/javascript">

    var variants = {};
    {% for variant in product.variants %}
      variants[{{variant.id}}] = {{ variant.metafields | json }};
    {% endfor %}

    console.log("VARIANTS", variants);
</script>

In the output i do not have metafields data. Does anyone know what the problem is?

Ad

Answer

You can use the json filter by referencing the namespace of the metafields that you would like to render. For example if your metafields are in the global namespace:

{% for variant in product.variants %}
  {{ variant.metafields.global | json }}
{% endfor %}

More on metafield namespaces here: https://help.shopify.com/themes/liquid/objects/metafield

Ad
source: stackoverflow.com
Ad