Den Kison
Den Kison

Reputation: 1084

Product variant do not have metafields in theme

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?

Upvotes: 1

Views: 447

Answers (1)

Josh Brown
Josh Brown

Reputation: 4096

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

Upvotes: 1

Related Questions