Metafields for variants not showing

adelinadk
Visitor
3 0 0

Hi 🙂 hope somebody here can help me out.

I am new to shopify, and I am trying to create metafields for my product variation. More specific, trying to get it to show a description for each product variation. 

 

I have spend a long time and now it finally shows up on the page with the following liquid code:

{%assign current_variant = product.selected_or_first_available_variant%}
{{current_variant.metafields.custom.varient_description}}

 

But the problem now is:

1) I do not know how to get the code to reloade and change the text for the variant, when you click to select a new variant. The only time the code above reloads is when you refresh the browser. How do I do it?

 

2) How do I get it to not show text, if the metafield data is not filled out on a product variation?

 

Thanks in advance!

Replies 2 (2)

tim
Shopify Expert
3305 247 1194

1) Liqiud code only runs on Shopify servers, and visitors browser receives a rendered HTML file which can then be updated by the theme Javascript code.

So, to update this field on your page you need to add some JS code to do it to your theme and this will depend on what theme you have.

 

2) you can do it like this:

 

{% assign current_variant = product.selected_or_first_available_variant %}
{% assign value = current_variant.metafields.custom.varient_description | strip %}
{% unless value == blank %}
  {{ value }}
{% endunless %}

 

If you  don't feel like coding you may consider an App for this:  https://apps.shopify.com/dynamic-variant-description 

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
adelinadk
Visitor
3 0 0

Oh thank you Tim. I am supposed to put that under the product–code file then?