Hide existing section when metafield exists

Hide existing section when metafield exists

Izzy5
Shopify Partner
12 0 2

Is there way to hide existing section when metaobject definition used. I have large product catalog and instead of creating different product templates, looking for the way where easy to apply some edits to code section file to display dynamic content if section exists with metaobject definition.  

 

Example before/after section used as metaobject with many different fields and want to display section only when metaobject definition exists else want to hide it.

Thanks.

Replies 4 (4)

Jan_Ole
Shopify Partner
24 4 9

Hey.

You can use this code:

{% if product.metafields.custom.yourMetafieldName.value != blank %}
// Your Section
{%- endif %}

This will make code in the if-statement only appear if the metafield is not empty.

Please note that you need to edit the theme in the right place to make this change work.

 

Greetings

Jan Ole Wiedenroth | Gründer & Inhaber | Wiedenroth Design
Besuche uns: wiedenroth-design.de
Leidenschaftlicher Digitalisierer | Expertise in E-Commerce, Webdesign, Webentwicklung und mehr.
Izzy5
Shopify Partner
12 0 2

It is for metafield  definition. Looking for metaobject as there are multiple fields included in it. 

Jan_Ole
Shopify Partner
24 4 9

Hey. 

I see. Sorry for the confusion.

 

You can use this code:

 

{% if product.metafields.custom.yourMetaObject %}
// Your section
{%- endif %}

 

 

To make this work, you first have to create a metaobject in content → metaobjects. After that, you have to assign the metaobject as a product metafield.

With the code I mentioned above, the section will show up when your product has a metaobject assigned.

Jan Ole Wiedenroth | Gründer & Inhaber | Wiedenroth Design
Besuche uns: wiedenroth-design.de
Leidenschaftlicher Digitalisierer | Expertise in E-Commerce, Webdesign, Webentwicklung und mehr.
lorrainedevaux
Tourist
10 0 5

Having a similar issue in an Image with Text section, and I just can't figure out where to place the "if" code mentioned above so that this section will not appear on my page when there are no values in these metafields.

 

"0c50a836-5c60-4c74-9a03-a4f198b3f2f0": {
"type": "image-with-text",
"blocks": {
"template--14834752159857__0c50a836-5c60-4c74-9a03-a4f198b3f2f0-heading-1": {
"type": "heading",
"settings": {
"heading": "{{ metaobject.event_title.value }}",
"heading_size": "h2"
}
},
"template--14834752159857__0c50a836-5c60-4c74-9a03-a4f198b3f2f0-text-1": {
"type": "text",
"settings": {
"text": "<p>{{ metaobject.event_date_and_time | metafield_tag }}<\/p>",
"text_style": "body"
}
},
"template--14834752159857__0c50a836-5c60-4c74-9a03-a4f198b3f2f0-button-1": {
"type": "button",
"disabled": true,
"settings": {
"button_label": "Button label",
"button_link": "",
"button_style_secondary": false
}
}