Expanse Theme | Hide link if filtered collection is empty

Due to markets some categories or filtered categories might be empty, so I am trying to update our header to not render these empty categories.

I have a collection called "shirt-and-trouser" this collection is setup to include all items with tags "shirt" and "trouser". One of my markets excludes "trouser", meaning that the "shirt-and-trouser" collection will only contain “shirt”.

My theme is iterating main_menu.links to build something like this

Shirts and Trousers
   Shirts
   Trousers

However, the child link for trousers will point to the collection shirt-and-trouser and the collection isn’t empty (it has "shirt"s in it) it will have a non-zero amount of products in it.

I how can I take tags into account when checking if a link should be visible?

I am using this snippet and using a render to only show the link if the collection is empty. Can this be changed to accommodate filtered collections?

{% assign render_link = true %}
{% if current_link.type == 'collection_link' and current_link.links == blank %}
  {% assign render_link = false %}
  {% for item in current_link.object.products %}
    {% if item.available %}
      {% assign render_link = true %}
    {% endif %}
  {% endfor %}
{% elsif current_link.type == 'collection_link' %}
  {% assign render_link = false %}
  {% for current_child_link in current_link.links %}
    {% for item in current_child_link.object.products %}
      {% if item.available %}          
        {% assign render_link = true %}
      {% endif %}
    {% endfor %}
  {% endfor %}
{% endif %}

{% if render_link == true %}
  <a href="{{ current_link.url }}" class="{{ current_class }}">
    {{ current_link.title }}
  </a>
{% endif %}

Thank you, it is as I expected then.

I will reorganize the website into “pure” collections then. It was initially done this way as to have nice breadcrumbs for navigation.

Collections / Shirts and Trousers / Shirts
                                  / Trousers