Re: How do I hide a metafield when it’s blank?

Solved

How do I hide a metafield when it’s blank?

bovinaceae
Tourist
5 0 2

I have a clothing store and I want to hide ‘size chart’ field on certain product page. I’m using Dawn theme. 

Accepted Solution (1)

PaulNewton
Shopify Partner
6722 610 1433

This is an accepted solution.

If it is a predefined block that uses a metafield definitions as a dynamic source then you will want to create an alternate template to use on the products that have no style guide.

https://help.shopify.com/en/manual/online-store/themes/os20/theme-structure/templates#create-a-new-t...

 

If you are using a a custom-liquid block|section simply use a liquid if tag to conditionally check if the metafield exists {% if product.metafields.information.size_guide %}.

 

Or is blank {% if product.metafields.information.size_guide != blank %}.

https://shopify.dev/api/liquid/basics#truthy-and-falsy-example 

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


View solution in original post

Replies 4 (4)

PaulNewton
Shopify Partner
6722 610 1433

This is an accepted solution.

If it is a predefined block that uses a metafield definitions as a dynamic source then you will want to create an alternate template to use on the products that have no style guide.

https://help.shopify.com/en/manual/online-store/themes/os20/theme-structure/templates#create-a-new-t...

 

If you are using a a custom-liquid block|section simply use a liquid if tag to conditionally check if the metafield exists {% if product.metafields.information.size_guide %}.

 

Or is blank {% if product.metafields.information.size_guide != blank %}.

https://shopify.dev/api/liquid/basics#truthy-and-falsy-example 

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


bovinaceae
Tourist
5 0 2

Yes that worked. Thanks for your help! 

MRamzan
Shopify Partner
213 3 31

Here is the best way I used to solve this problem:

 

Step 1: Add "Custom liquid/Custom HTML" block instead of Text block.

Step 2: Go to Dashboard -> Settings -> Custom data -> Products

Step 3: See all the keys generated to access your metafields example: "product.metafields.custom.gemstone_size"

 

Step 4: Go to Dashboard -> Customize

Step 5: Go to product or any other page where you are displaying metafields (in my case I need on product page)

Step 6: For individual metafields to display, just add "Custom liquid" block and put the conditions like this:

 

{%- if product.metafields.custom.gemstone_size != blank -%}
<strong>Gemstone size (cm):</strong> {{ product.metafields.custom.gemstone_size }}
{%- endif -%}

 

In your case the keys for displaying metafields will be different. For each metafield to display you need as many "Custom liquid" blocks and need to put conditions accordingly.

 

Hope! I answered the question.

Thanks

Hire Me:

WhatsApp: +91-9145985880
Email: mohdramzaan112@gmail.com
Skype: mohdramzaan112
Katerina000
Visitor
1 0 0

This is very heplful, thank you!