Solved

Why are my hidden empty meta fields showing again?

Mellowsilcbd
Tourist
14 0 1

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?

Accepted Solution (1)
RhiRo
Shopify Partner
41 3 8

This is an accepted solution.

Sorry! please do 

{% if block.settings.content != "" %}
I've tested this locally so hopefully this should work for you 🙂 

View solution in original post

Replies 27 (27)

RhiRo
Shopify Partner
41 3 8

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.

Mellowsilcbd
Tourist
14 0 1

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?

RhiRo
Shopify Partner
41 3 8

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 🙂 

Mellowsilcbd
Tourist
14 0 1

the script is added above the <head>
image.png

RhiRo
Shopify Partner
41 3 8

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 👍

Mellowsilcbd
Tourist
14 0 1

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

 

RhiRo
Shopify Partner
41 3 8

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) }} 

Mellowsilcbd
Tourist
14 0 1

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
image.png

Mellowsilcbd
Tourist
14 0 1

i added the metafields in the admin settingsimage.png
maybe this helps

RhiRo
Shopify Partner
41 3 8

Ah okay - do you link to them in the theme editor then? 

(And not stupid at all!)

 

Mellowsilcbd
Tourist
14 0 1

yes thats exactly how i do it. the shopif support advised me to do so image.png

RhiRo
Shopify Partner
41 3 8

Ohh yes great, so in the liquid file if you search for 

{%- when 'collapsible_tab' -%}
I'm not sure exactly what yours will look like, as we use dawn but have made a lot of changes over time 

{%- when 'collapsible_tab' -%}
{% if block.settings.content != null %}
<div class="product__accordion accordion" {{ block.shopify_attributes }}>
more code  .. 
</div>
{% endif %}
{%- when 'reviews-io' -%} - This might not be your next block, but you'll just want the endif before the next 'when' 
 
You will want to add these bold bits in 🙂 
Mellowsilcbd
Tourist
14 0 1

First Thank you very much.

i just have to ask which liquid file exactly do you mean?

RhiRo
Shopify Partner
41 3 8

Hi Mellowsilcbd, 

In the main-product.liquid file 🙂 

Mellowsilcbd
Tourist
14 0 1

Thank you very much so now it should look like this?image.png

RhiRo
Shopify Partner
41 3 8

Just move that {% endif %} down two lines, so it is above the {% when 'quantity_selector' %} 🙂 

 

Mellowsilcbd
Tourist
14 0 1

i changed it but sadly it isnt working 😞

RhiRo
Shopify Partner
41 3 8

Sorry 🥲 I'll take a look

Mellowsilcbd
Tourist
14 0 1

now all my Metafields are gone not only the empty once.

RhiRo
Shopify Partner
41 3 8

This is an accepted solution.

Sorry! please do 

{% if block.settings.content != "" %}
I've tested this locally so hopefully this should work for you 🙂 
Mellowsilcbd
Tourist
14 0 1

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!

Mellowsilcbd
Tourist
14 0 1

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!

RhiRo
Shopify Partner
41 3 8

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 🙂 

jess94
Shopify Partner
1 0 0

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!

Eugene13
Excursionist
13 0 4

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

1080
Shopify Partner
296 9 51

@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 %}
Mellowsilcbd
Tourist
14 0 1

The code is added in the theme.liquid section 

the meta field is created in products.