cancel
Showing results for 
Search instead for 
Did you mean: 

How to display category filter on layered navigation in Magento 2?

SOLVED

How to display category filter on layered navigation in Magento 2?

 

Hello,

 

How can i display sub categories into layered navigation while redirect into category page from navigation menu ?

I enabled yes into Anchor field of Manage category (backend) but still it's not displaying into frontend.

image.png

Magento Version : 2.2.6

Theme : Ves Fasony free version and it's brand module also
Please suggest me how can i solve it ?

 

Thanks in advance 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to display category filter on layered navigation in Magento 2?

Otherwise, you can override category_filter.phtml template file in your theme, 

 

<!--Product List page side bar section -->
<div class="side-products" data-mage-init='{"productSideBar":{"productSideBarSelector": ".category-sidebar-title"}}'>
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryD = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
$catPath = $categoryD->getPath();//Get Current Category
$explodedPath = explode("/", $catPath);
$parentCategoryId = $explodedPath[2];
$category = $objectManager->get('Magento\Catalog\Model\Category')->load($parentCategoryId);
?>
<?php if ($category): ?>
    <?php
    $subcats = $category->getChildrenCategories();
    if (count($subcats) > 0) {
        ?>
        <?php foreach ($subcats as $cat) { ?>
            <div class="block-category-list dckap-list">
                <?php $subsubcats = $cat->getChildrenCategories();
                $class = "";
                if (count($subsubcats) < 1) $class = "hide-arrow";
                ?>
                <div class="block-title <?php echo $class; ?> category-sidebar-title">
                    <a href="<?php echo $cat->getUrl() ?>"
                    <strong><?php echo $cat->getName() ?></strong>
                    </a>
                </div>
                <div class="block-content category-sidebar-content">


                    <?php if (count($subsubcats) > 0) {
                        ?>
                        <ol class="items">
                            <?php
                            foreach ($subsubcats as $subsubcat) {
                                ?>
                                <li class="item">
                                    <a href="<?php echo $subsubcat->getUrl() ?>"><?php echo $subsubcat->getName() ?></a>
                                </li>
                                <?php
                            }
                            ?>
                        </ol>
                    <?php } ?>
                </div>
            </div> <?php } ?>
        <script type="text/javascript">
            require([
                'jquery'
            ], function ($) {
                $("#layered-filter-block").before($(".block.block-category-list"));
            });
        </script>
        <?php
    }
    ?>
<?php endif; ?>
</div>

View solution in original post

2 REPLIES 2

Re: How to display category filter on layered navigation in Magento 2?

Need to override following model :

Magento\CatalogSearch\Model\Layer\Filter\Category

Override by creating new Module and in di.xml file by following way :

<preference for="Magento\CatalogSearch\Model\Layer\Filter\Category" type="Vendor\Module\Model\Layer\Filter\Category" />

 

Reference : https://magento.stackexchange.com/questions/175841/magento-2-how-to-show-all-active-categories-in-la...

Re: How to display category filter on layered navigation in Magento 2?

Otherwise, you can override category_filter.phtml template file in your theme, 

 

<!--Product List page side bar section -->
<div class="side-products" data-mage-init='{"productSideBar":{"productSideBarSelector": ".category-sidebar-title"}}'>
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryD = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
$catPath = $categoryD->getPath();//Get Current Category
$explodedPath = explode("/", $catPath);
$parentCategoryId = $explodedPath[2];
$category = $objectManager->get('Magento\Catalog\Model\Category')->load($parentCategoryId);
?>
<?php if ($category): ?>
    <?php
    $subcats = $category->getChildrenCategories();
    if (count($subcats) > 0) {
        ?>
        <?php foreach ($subcats as $cat) { ?>
            <div class="block-category-list dckap-list">
                <?php $subsubcats = $cat->getChildrenCategories();
                $class = "";
                if (count($subsubcats) < 1) $class = "hide-arrow";
                ?>
                <div class="block-title <?php echo $class; ?> category-sidebar-title">
                    <a href="<?php echo $cat->getUrl() ?>"
                    <strong><?php echo $cat->getName() ?></strong>
                    </a>
                </div>
                <div class="block-content category-sidebar-content">


                    <?php if (count($subsubcats) > 0) {
                        ?>
                        <ol class="items">
                            <?php
                            foreach ($subsubcats as $subsubcat) {
                                ?>
                                <li class="item">
                                    <a href="<?php echo $subsubcat->getUrl() ?>"><?php echo $subsubcat->getName() ?></a>
                                </li>
                                <?php
                            }
                            ?>
                        </ol>
                    <?php } ?>
                </div>
            </div> <?php } ?>
        <script type="text/javascript">
            require([
                'jquery'
            ], function ($) {
                $("#layered-filter-block").before($(".block.block-category-list"));
            });
        </script>
        <?php
    }
    ?>
<?php endif; ?>
</div>