Why is my metafield if statement not functioning properly?

Hi,

I’m having issues with this - needing to hide a snippet if a metafield is blank.

Firstly I tried this method:

{% if product.metafields.my_fields.sku != blank %}
{% else %}
{% render ‘text-with-icon’, icon: block.settings.icon, text: block.settings.text, link: block.settings.link %}
{% endif %}

I got the blank on everything - the content didn’t appear even when the metafield was filled.

I then tested to make sure that the string was correct, which it was:

{{ product.metafields.my_fields.sku }}

Because that didn’t work, I tried the opposite of blank:

{% if product.metafields.my_fields.sku == true %}
{% render ‘text-with-icon’, icon: block.settings.icon, text: block.settings.text, link: block.settings.link %}
{% endif %}

Still not having any joy. Looking at previous posts, it looks like some have resolved this, but I can’t get it to work any which way. Help please!

Hello @KiwiDesigner ,

We would like to inform you that the condition you have provided is wrong.

Here you have provided a condition to render “blank” when the metafield value is filled, causing the issue.

The correct format is provided below:

{% if product.metafields.my_fields.sku != blank %}

{% render ‘text-with-icon’, icon: block.settings.icon, text: block.settings.text, link: block.settings.link %}

{% endif %}

If you still face issues fixing this, please connect with us.

All the best,

Team CedCommerce