Using Variant metafields on product card

nguzad
Excursionist
17 0 9

Hello, I'm trying to display variant metafields onto my product swatch in the Dawn theme. Product metafields work well with {{ product_card_product.metafields.namespace.key }} but I can't figure out the method for variant metafields. According to docs, I tried {{ variant.metafields.namespace.key }} but this doesn't work neither have any other variations I have tried..

 

 

Replies 2 (2)

PaulNewton
Shopify Partner
6274 573 1319

Variants are of a product. That's plural so either you must or select a specific one, or iterate through each variant of that product.

 

If an object, i.e. product , has been given another name follow that naming to get it's children objects.

In the below it's getting the first variant from the variants array with dot notation, or the selected variant.

{{ product_card_product.metafields.namespace.key }}

{{ product_card_product.variants.first.metafields.namespace.key }} 

{{ product_card_product.selected_variant.metafields.namespace.key }}

 

Iterating with a variant forloop left as exercise for the reader.

{% forloop variant in product_card_product.variants %}
 {{ variant.metafields.namespace.key }} 
{% endfor %}

 

Dynamic interaction from variant-option selectors requires JavaScript an advanced customization.

 

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


nguzad
Excursionist
17 0 9
Hi, I have already tried to iterate over variants with the for loop. For
some reason this works perfectly fine with product metafields but I am
really struggling with variant metafields.