Home > Blockchain >  Luquid check if metafield exists
Luquid check if metafield exists

Time:07-21

I want to check if a metafield (created with metafields Guru) exists on a product or not and based on that change a variable to show or hide the div holding the metafield.

I got this:

{% assign display = "inline-block" %}

{% if product.metafields.energielabel.energie.value  == blank %}
{% assign display = "none" %}
{% endif %}

<div id="energielabel-container"><img  src="https://cdn.shopify.com/s/files/1/0619/8034/4516/{{ product.metafields.energielabel.energie.value }}" /></div>


<style>
#energielabel-container {
width: 80px;
display: {{ display }};
}


</style>

When I just add some example letters into to the if condition it´s working as it should (empty metafield = shows the exaple letters ), but redefining the variable doesn´t work...

CodePudding user response:

Try:

{% if product.metafields.energielabel.energie.value != blank %}
  {% assign display = "none" %}
{% endif %}

CodePudding user response:

{% unless product.metafields.energielabel.energie %}
    {% assign display = "none" %}
{% endunless %}
  • Related