How to display a list if not empty ONLY after excluding specific list-items

Solved
New Member
8 0 0

Its an awkward question to summarise in the title. 

Essentially I am using liquid to display a list of metafields excluding a few unwanted fields.


{% for field in product.metafields.global %}

{% assign dontInclude = field | first %}

{% unless dontInclude == 'call-for-price' 
or dontInclude == 'visibility'
or dontInclude == 'short-description' 
%} 

{{ field | first }}

{% endunless %}
{% endfor %}



However I want to apply code elsewhere to only display this list element if its not empty ONLY after excluding the specified fields. (Currently I can check if the metafields are empty but it will pick up the fields I don't want to show and it will consider the metafields to be not empty however on the page it will be empty as I have excluded specific fields from showing)

0 Likes
Shopify Partner
1137 91 138

Hi @RayLyons 

 

I need more clarity for this !! Unable to get what you want from community ?

Thanks & Regards
Perennial Solution

Problem Solved?✔️Like and Accept the solution that can help others.
Need any help with theme customization or design? Hire us
Email: infoperennialsolution@gmail.com
Skype: infoperennialsolution
0 Likes
New Member
8 0 0

I need clarity on what you need clarity on! :)

Ok so I'll just give more background to the situation. 

We have a section where we are displaying a list of product metafields on the product page however some of the fields we do not want to display so we exclude them using the code shown above.

We only want to display the section if their are metafields to be displayed.

Currently we know how to apply a liquid if statement on the section to only display it if the metafields are not empty however this does not work as all products have the metafields we are hiding using the liquid above. 

How do we apply an if statement to only show the section if there are no metafields to be shown AFTER the specified metafields are excluded?

0 Likes

Success.

Astronaut
1081 146 272

You could loop through and get a count in your unless statement. If the count is greater than 0, then you can loop through again and use your same statement from before:

 

{% assign count = 0 %}
{% for field in product.metafields.global %}
  {% assign dontInclude = field | first %}

  {% unless dontInclude == 'call-for-price' or dontInclude == 'visibility' or dontInclude == 'short-description' %} 
    {% assign count = count | plus: 1 %}
  {% endunless %}
{% endfor %}
  
{% if count > 0 %}
  {% for field in product.metafields.global %}
    {% assign dontInclude = field | first %}
    {% unless dontInclude == 'call-for-price' or dontInclude == 'visibility' or dontInclude == 'short-description' %} 
      {{ field | first}}
    {% endunless %}
  {% endfor %}
{% endif %}

Did I interpret what you're looking for clearly? This will output any tag that arent the tags you're identifying in the unless statement, but wont output anything if ONLY one or more of them exist and no other metafield besides them in the global namespace.

If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
1 Like
New Member
8 0 0

Great! this is exactly what I needed. Thanks for your help.

0 Likes
Astronaut
1081 146 272

No problem, if you could accept it as the solution to help anyone else who might face the same problem it'd be appreciated. Have a good one.

If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
0 Likes