Hide parent link that consist of 0 products

Hide parent link that consist of 0 products

In mega-menu.liquid, add the followings:

{% for link in parent_link.links %}

Notes
{% liquid
# Check if this child link has any visible grandchildren with products
assign has_visible_items = false
if link.links.size > 0
for childLink in link.links
if childLink.object.products_count > 0
assign has_visible_items = true
break
endif
endfor
else
# If no nested links, check the link itself
if link.object.products_count > 0
assign has_visible_items = true
endif
endif
%}
{% if has_visible_items %}

{% liquid
# Decide whether to open a column span, and how many columns of the grid to span
if forloop.first or open_column_span == false
assign open_column_span = true

# Don't allow orphan links in a column
assign break_after_modulo = link.links.size | modulo: links_before_wrap
if break_after_modulo == 1
assign links_before_wrap = links_before_wrap | minus: 1
endif
assign break_after_float = links_before_wrap | times: 1.0
assign column_span = link.links.size | divided_by: break_after_float | ceil | at_least: 1
assign column_count = column_count | plus: column_span
assign should_break_columns = true

if menu_content_type == 'collection_images'
assign should_break_columns = false
# For collection image mode, we have an 8-column grid when 4 or fewer parent links are used. Set column span to 2 in that case, otherwise use 1 column
if parent_link.links.size <= 4
assign span_class = 'mega-menu__column--wide-collection-image'
else
assign span_class = 'mega-menu__column--span-1'
endif
else
# For all other modes, column span is determined by the number of links in the parent link
assign span_class = 'mega-menu__column--span-' | append: column_span
endif
echo '<li class="mega-menu__column ' | append: span_class | append: '">'
endif
%}
<div>
<a
href="{{ link.url }}"
class="mega-menu__link {% if link.links != blank %}mega-menu__link--parent{% endif %}"
>
{% if menu_content_type == 'collection_images' %}
{%- capture collection_image_sizes -%}
{%- if parent_link.links.size <= 4 -%}
{%- assign columns_per_item = 2 -%}
{%- else -%}
{%- assign columns_per_item = 1 -%}
{%- endif -%}
{%- render 'util-mega-menu-img-sizes-attr',
menu_content_type: 'collection_images',
settings: settings,
grid_columns: grid_columns_count,
grid_columns_tablet: grid_columns_count_tablet,
grid_columns_collection_images: grid_columns_count_collection_images,
parent_links_size: parent_link.links.size,
columns_per_item: columns_per_item
-%}
{%- endcapture -%}

{% render 'link-featured-image', link: link, class: 'mega-menu__link-image', sizes: collection_image_sizes %}
{% endif %}
<span
class="mega-menu__link-title wrap-text"
>
{{- link.title -}}
</span>
</a>
{% if link.links != blank %}
<ul
class="list-unstyled"
{% if should_break_columns %}
style="column-count: {{ column_span }};"
{% endif %}
>
{% for childLink in link.links %}
{% if childLink.object.products_count > 0 %}
{% assign break_after_modulo = forloop.index | modulo: links_before_wrap %}
<li
{% if break_after_modulo == 0 and should_break_columns %}
style="break-after: column;"
{% endif %}
>
<a
href="{{ childLink.url }}"
class="mega-menu__link"
>
<span class="mega-menu__link-title wrap-text">{{- childLink.title -}}</span>
</a>
</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
</div>

Notes
{% endif %}


In header-drawer.liquid, add the followings;

{%- for childlink in link.links -%}

Notes
{% liquid
# Check if this childlink has any visible grandchildren with products
assign has_visible_grandchildren = false
if childlink.links.size > 0
for grandchildlink in childlink.links
if grandchildlink.object.products_count > 0
assign has_visible_grandchildren = true
break
endif
endfor
else
# If no grandchildren, check the childlink itself
if childlink.object.products_count > 0
assign has_visible_grandchildren = true
endif
endif
%}
{% if has_visible_grandchildren %}

<li
class="
menu-drawer__list-item

{% if childlink.links != blank -%}

{% if block_settings.drawer_accordion -%} menu-drawer__list-item--deep{% else -%} menu-drawer__list-item--flat{%- endif -%}

{%- endif -%}

{% if block_settings.drawer_dividers %} menu-drawer__list-item--divider{% endif %}
"
>
{%- if childlink.links == blank -%}