Shopify themes, liquid, logos, and UX
Hey
iam using Meta fields to show ingredients, shipping details and how to use the product. But for some there are no such informations i looked for a solution to hide the empty meta fields in the frontend. From a different Disscusion i found this code from this discussion Solved: How to hide blank metafields. - Shopify Community
<script>
const interval = setInterval(() => {
const blocks = document.querySelectorAll('.product__accordion.accordion.quick-add-hidden')
let allMetafieldsEmpty = true
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i]
const blockApp = block.querySelector('.accordion__content')
if (blockApp) {
if (blockApp.childElementCount === 0) {
blockApp.parentElement.remove()
} else {
allMetafieldsEmpty = false
}
}
}
if (allMetafieldsEmpty) {
clearInterval(interval)
}
}, 100)
</script>
I implemented the code and it worked perfectly fine but one day later it didnt worked but i didnt changed anthing on the code.
do you have any idea why this is happening?
Solved! Go to the solution
This is an accepted solution.
Sorry! please do
Can I ask why you're using javascript and not liquid?
With liquid, you can wrap the html that you don't want to show if it's empty with an if statement -
eg:
{% if product.metafield.namespace.key != blank %}
<div>
<h1> Here is a metafield bit</h1>
{{ product.metafield.namespace.key }}
</div>
{% endif %}
If it's one block containing multiple metafield references and you want to remove it if there is nothing there, you can just use or in the if statement
eg
{% if product.metafield.namespace.key != blank or product.metafield.namespace2.key2 != blank%}
<div>
<h1> Here is a metafield bit</h1>
{{ product.metafield.namespace.key }}
{{ product.metafield.namespace2.key2 }}
</div>
{% endif %}
This means it will only show if metafield 1 or metafield 2 (or both) exists.
First of all thank you for the Reply.
i copied the code from another solution. but i paste it into the theme.liquid area
where and how do i have ti implement ur code to check?
Hi Mellowsilcbd,
Could you show us in your liquid file where the metafields are referenced - ie the bit where you show the ingredients
Thanks
Rhianna 🙂
the script is added above the <head>
Hi Mellowsilcbd,
No, sorry I mean in the main-product.liquid (or similar, not sure what theme you're using) 🙂
The bit on your product page where you reference the ingredients metafield 👍
Hi Rhianna,
do you mean on the "Frontend" of the Store what the Customer sees or in the Backend in the Section Products?
if it helps iam using the Dawn Theme
Hi Mellowsilcbd,
I mean the code in shopify, not what the customer sees 🙂
Not important right now, but this is all classed as 'Frontend' in web development haha 😄
That does help, thank you.
I just need to see in your code where it says {{ product.metafields. (and then what your metafield is called) }}
Hey i think iam pretty stupid right now haha but this is the only thing i can find with {{product.metafields but rating isnt what my ingridents metafield is called
Iam pretty desperate. Iam sorry iam acting so stupid right now haha
i added the metafields in the admin settings
maybe this helps
Ah okay - do you link to them in the theme editor then?
(And not stupid at all!)
yes thats exactly how i do it. the shopif support advised me to do so
Ohh yes great, so in the liquid file if you search for
First Thank you very much.
i just have to ask which liquid file exactly do you mean?
Hi Mellowsilcbd,
In the main-product.liquid file 🙂
Thank you very much so now it should look like this?
Just move that {% endif %} down two lines, so it is above the {% when 'quantity_selector' %} 🙂
i changed it but sadly it isnt working 😞
Sorry 🥲 I'll take a look
now all my Metafields are gone not only the empty once.
This is an accepted solution.
Sorry! please do
Thank you soo much.
it was a rough ride but its working. i hope it will stay this way and wont change like the last thing i tried which stopped working the next day
Thank you very very much for the Support!
Thank you very much its Working.
I hope this will hold up because the last think i tried worked for one day and then it didnt.
Thank you very very much it was a rough ride but you helped me soo much!
Thanks Mellowsilcbd!
Yes sorry it took a little while and that my previous answer was a bit wrong! Technically the content is not "null" as it has the metafield reference i think, even if that metafield is empty. Glad it is working for you now 🙂
Unless you change anything, this should continue to work - we use many if statements in our code reliably.
I wouldn't recommending using Javascript when it is possible to use liquid, it requires the content to load before removing it, so can require extra load time (small in this case) and some people have javascript disabled etc. When using liquid, it means the removal is done first 🙂
Hi @RhiRo ! Thanks for your help to Mellowsilcbd, it works for me too.
Question, how can I make this work too if the block has more than 1 metafields though? Appreciate your help!
Hi RhiRo.
I just want to ask if the multicolumn will be hide if the metafield is empty? I have 3 columns saying power, torque and weight saving. I just want to show those 3 columns if they have a metafield value
@Mellowsilcbd where you this code added ?
where you create the meta filed ?
I will suggest to when you rener the metafiled that time you can check with
{% if product.meta. %} {% endif %}
The code is added in the theme.liquid section
the meta field is created in products.
Hey Community 👋 Did you know that March 15th is National Everything You Think Is W...
By JasonH Apr 1, 2025Discover how to increase the efficiency of commerce operations with Shopify Academy's l...
By Jacqui Mar 26, 2025Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025