Search code examples
shopware6

How to display current parent active category and its sub-categories only in Sidebar?


Hey I would like to display only active parent category and its available sub-categories in the category-tree in storefront/layout/sidebar/category-navigation.html.twig. so if category A is selected then it should display all its subcategories. example

Category A

  • Category A.1
  • Category A.2
  • Category A.3

Solution

  • This will output the tree branch to the currently active category, as well as the links to its direct descendant categories.

    {% sw_extends '@Storefront/storefront/layout/sidebar/category-navigation.html.twig' %}
    
    {% block layout_navigation_categories_list_entry %}
        {% if (item.category.id in activeResult.id) or (item.category.id in activeResult.path) or (item.category.parentId in activeResult.id) %}
            {{ parent() }}
        {% endif %}
    {% endblock %}
    

    Update: If you want to hide the parents up to the currently viewed category, this should do the trick.

    {% block layout_navigation_categories_list_entry %}
        {% if item.category.id in activeResult.path %}
            {% block layout_navigation_categories_recoursion %}
                {% if item.category.id in activeResult.id %}
                    {% set levelIncrement = 1 %}
                {% else %}
                    {% set levelIncrement = 0 %}
                {% endif %}
                {% sw_include '@Storefront/storefront/layout/sidebar/category-navigation.html.twig' with {
                    navigationTree: item.children,
                    activeResult: activeResult,
                    level: level + levelIncrement
                } only %}
            {% endblock %}
        {% endif %}
        {% if (item.category.id in activeResult.id) or (item.category.parentId in activeResult.id) %}
            {{ parent() }}
        {% endif %}
    {% endblock %}