Code to hide Content Block when dynamic content (product metafield) is empy

I’m trying to add a bit of content, populated by a product metafield, so some of my product templates. If the metafield is blank, I want to hide this content block instead of showing a link that does nothing.

I found an existing topic that provided an if/endif wrap for the content block, but I’m getting a json error when trying to save. Can anyone tell me where I’m going wrong?

Here is the code, with the section I’ve been editing in bold:

},

   "variant_picker": {

     "type": "variant_picker",

     "settings": {

       "product_variant_style": "swatches-buttons",

       "popup_enable": false,

       "popup_trigger": "Size\\nGröße\\nTaille\\nTaglia\\nTalla",

       "popup_label": "Size guide",

       "popup_content": ""

     }

   },

   **{% if product.metafields.custom.about_brand.value != blank %}**

   **"link_DwCCVf": {**

     **"type": "link",**

     **"settings": {**

       **"column": "primary",**

       **"title": "View Geometry Chart",**

       **"link": "{{ product.metafields.custom.geometry_chart.value }}",**

       **"style": "body",**

       **"align": "center",**

       **"new_tab": false**

     **}**

     **{% endif %}**

   },

   "tab_MjWfmz": {

     "type": "tab",

     "disabled": true,

     "settings": {

       "column": "primary",

       "title": "Need Another Size?",

       "icon": "icon-question-circle",

       "icon_size": 2,

Thanks for any help!

ah, apply bold to the code has messed that up a bit – lines with ** are the lines in question, the asterisks are not present in my active code!

Can you share the link to the existing topic?

I don’t think you can include liquid inside a json, which would be why you’re getting the error.

Normally, you’d do the liquid in your page, like this_product.liquid or similar and then check if the metafield is blank. I’m going to assume you want a linked value to appear if the metafield is blank.

I guess it would then look something like this:

{% if product.metafields.custom.about_brand.value != blank %}
  <p><a href="{{ product.metafields.custom.geometry_chart.value }}">View Geometry Chart</a></p>
{% endif %}

I’ve set it as a hyperlink, because “yes”. This would not appear if the metafield is blank.

It might be a bit better/easier if you can give us a link to your site, set 2 instances up (1 with the metafield and 1 without), show us a screen shot with what should or should not appear.

Happy to help!

Nathan

Such conditional check needs to go into the snippet/block itself that used liquid syntax for sanity.
A kludge is to give the block a custom-liquid TYPE setting and give that the conditional check the spits out some CSS to visually hide empty elements WITHOUT accidently hiding every other element.

Settings files are JSON, you can’t insert arbitrary liquid into arbitrary JSON.
In no place, for a shopify theme setting, should you have encountered code wrapping liquid around JSON like this having you mix languages like that .
When you have seen {% if thing != blank %} it should have been in the context of liquid template being wrapped not JSON and you chose to go off path and intermingle two separate things.

Some JSON conditional settings values, the parts in quotes “”, may accept liquid syntax as a string.
But settings like visibility unfortunately can’t process if some property is blank are are meant to SETTINGS logic not CONTENT logic.

Reach out if you need this fixed properly or consulting on shopify themes.

The JSON error is coming from the Liquid tags sitting inside the schema object. In a section schema, Shopify expects valid JSON only, so `{% if %}` and `{% endif %}` can’t wrap a block definition like that.

The usual fix is to keep the block in the JSON, then control whether it renders in the section template or block render file. For example, render the link only when the metafield has a value:

{% if product.metafields.custom.geometry_chart.value != blank %}
<a/ href=“{{ product.metafields.custom.geometry_chart.value }}”>View Geometry Chart<//a>
{% endif %}

If you want the block to disappear from the editor entirely when the metafield is empty, that’s not something the schema can do conditionally. You’d need to make the block always exist in JSON and handle the visibility in Liquid.

Also, you’re checking `about_brand` in the if statement but outputting `geometry_chart` in the link, so make sure you’re testing the same metafield you’re actually using. That mismatch alone can make it look like the logic is broken.

Are you trying to hide the link on the product page only, or remove the block from the theme editor too?

Hi,

You must add this code to the liquid section, not the schema on file.

Best regards,
Dan from Ryviu: Product Reviews & Loyalty app