4

These are Categories List in admin:

Admin Categories List

Categories Showing in Layered Navigation on Shop Parts Category Page:

Categories in Layered Navigation

Now you can see only first child categories of shop parts category are showing and I want to show them as well as all children of those categories.

How can I achieve this functionality?

4
  • You can use this plugin: github.com/Sebwite/magento2-category-sidebar. You will have to enable flat category mode in configuration to display subcategories. After enabling flat category mode do not forget to run reindex command.
    – Sejal Shah
    Commented May 24, 2017 at 9:49
  • @SejalShah Installed this plugin and enabled flat category mode also reindex then only showing default category.Not Working properly. Commented May 24, 2017 at 15:08
  • @Nitesh i have also installed this but not working. Have you find any other solution?
    – Magecode
    Commented Oct 28, 2017 at 11:19
  • @Magecode Please see my answer and If you need more explaination please ask. Commented Oct 29, 2017 at 12:59

1 Answer 1

7

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" />

Below is my overrided Model :

NOTE:Following code is for 6-Level Categories If You have less or more level categories then use foreach loop as requirement (I mention level in each for loop)

<?php

 namespace Vendor\Module\Model\Layer\Filter;

  use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
  use Magento\Catalog\Model\Layer\Filter\DataProvider\Category as     CategoryDataProvider;

 class Category extends AbstractFilter
 {
/**
 * @var \Magento\Framework\Escaper
 */
private $escaper;

/**
 * @var CategoryDataProvider
 */
private $dataProvider;
protected $_logger;
/**
 * @param \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory
 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
 * @param \Magento\Catalog\Model\Layer $layer
 * @param \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder
 * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
 * @param \Magento\Framework\Escaper $escaper
 * @param CategoryManagerFactory $categoryManager
 * @param array $data
 */
public function __construct(   
    \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Catalog\Model\Layer $layer,
    \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder,
    \Magento\Framework\Escaper $escaper,
    \Magento\Catalog\Model\Layer\Filter\DataProvider\CategoryFactory $categoryDataProviderFactory,
    \Psr\Log\LoggerInterface $logger, //log injection

    array $data = []
) {
    parent::__construct(
        $filterItemFactory,
        $storeManager,
        $layer,
        $itemDataBuilder,
        $data
    );
    $this->escaper = $escaper;
    $this->_requestVar = 'cat';
    $this->dataProvider = $categoryDataProviderFactory->create(['layer' => $this->getLayer()]);
    $this->_logger = $logger;
}

/**
 * Apply category filter to product collection
 *
 * @param   \Magento\Framework\App\RequestInterface $request
 * @return  $this
 */
public function apply(\Magento\Framework\App\RequestInterface $request)
{
    $categoryId = $request->getParam($this->_requestVar) ?: $request->getParam('id');
    if (empty($categoryId)) {
        return $this;
    }

    $this->dataProvider->setCategoryId($categoryId);

    $category = $this->dataProvider->getCategory();

    $this->getLayer()->getProductCollection()->addCategoryFilter($category);

    if ($request->getParam('id') != $category->getId() && $this->dataProvider->isValid()) {
        $this->getLayer()->getState()->addFilter($this->_createItem($category->getName(), $categoryId));
    }
    return $this;
}

/**
 * Get filter value for reset current filter state
 *
 * @return mixed|null
 */
public function getResetValue()
{
    return $this->dataProvider->getResetValue();
}

/**
 * Get filter name
 *
 * @return \Magento\Framework\Phrase
 */
public function getName()
{
    return __('Category');
}

/**
 * Get data array for building category filter items
 *
 * @return array
 */
protected function _getItemsData()
{
    $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productCollection = $this->getLayer()->getProductCollection();

    $category = $this->dataProvider->getCategory();

    $optionsFacetedData = $productCollection->getFacetedData('category');
    $categories = $category->getChildrenCategories();
    $collectionSize = $productCollection->getSize();

    if ($category->getIsActive()) {
        foreach ($categories as $category) {
            if ($category->getIsActive()
                && isset($optionsFacetedData[$category->getId()])
                && $this->isOptionReducesResults($optionsFacetedData[$category->getId()]['count'], $collectionSize)
            ) {
                $this->itemDataBuilder->addItemData(
                    $this->escaper->escapeHtml($category->getName().'%level-1%'.$category->getParentId()),
                    $category->getId(),
                    $optionsFacetedData[$category->getId()]['count']
                );
            }
                $cat = $_objectManager->create('Magento\Catalog\Model\Category')->load($category->getId());
                $sub_category = $cat->getChildrenCategories();

                foreach ($sub_category as $category) {
                    if ($category->getIsActive()
                            && isset($optionsFacetedData[$category->getId()])
                            && $this->isOptionReducesResults($optionsFacetedData[$category->getId()]['count'], $collectionSize)
                        ) 
                    {
                        $this->itemDataBuilder->addItemData(
                            $this->escaper->escapeHtml($category->getName().'%level-2%'.$category->getParentId()),
                            $category->getId(),
                            $optionsFacetedData[$category->getId()]['count']
                        );
                    }
                    $cat = $_objectManager->create('Magento\Catalog\Model\Category')->load($category->getId());
                    $sub_category = $cat->getChildrenCategories();

                    foreach ($sub_category as $category) {
                        if ($category->getIsActive()
                                && isset($optionsFacetedData[$category->getId()])
                                && $this->isOptionReducesResults($optionsFacetedData[$category->getId()]['count'], $collectionSize)
                            ) 
                        {
                            $this->itemDataBuilder->addItemData(
                                $this->escaper->escapeHtml($category->getName().'%level-3%'.$category->getParentId()),
                                $category->getId(),
                                $optionsFacetedData[$category->getId()]['count']
                            );
                        }

                        $cat = $_objectManager->create('Magento\Catalog\Model\Category')->load($category->getId());
                        $sub_category = $cat->getChildrenCategories();

                        foreach ($sub_category as $category) {
                            if ($category->getIsActive()
                                    && isset($optionsFacetedData[$category->getId()])
                                    && $this->isOptionReducesResults($optionsFacetedData[$category->getId()]['count'], $collectionSize)
                                ) 
                            {
                                $this->itemDataBuilder->addItemData(
                                    $this->escaper->escapeHtml($category->getName().'%level-4%'.$category->getParentId()),
                                    $category->getId(),
                                    $optionsFacetedData[$category->getId()]['count']
                                );
                            }

                            $cat = $_objectManager->create('Magento\Catalog\Model\Category')->load($category->getId());
                            $sub_category = $cat->getChildrenCategories();

                            foreach ($sub_category as $category) {
                                if ($category->getIsActive()
                                        && isset($optionsFacetedData[$category->getId()])
                                        && $this->isOptionReducesResults($optionsFacetedData[$category->getId()]['count'], $collectionSize)
                                    ) 
                                {
                                    $this->itemDataBuilder->addItemData(
                                        $this->escaper->escapeHtml($category->getName().'%level-5%'.$category->getParentId()),
                                        $category->getId(),
                                        $optionsFacetedData[$category->getId()]['count']
                                    );
                                }

                                        $cat = $_objectManager->create('Magento\Catalog\Model\Category')->load($category->getId());
                                        $sub_category = $cat->getChildrenCategories();

                                        foreach ($sub_category as $category) {
                                            if ($category->getIsActive()
                                                    && isset($optionsFacetedData[$category->getId()])
                                                    && $this->isOptionReducesResults($optionsFacetedData[$category->getId()]['count'], $collectionSize)
                                                ) 
                                            {
                                                $this->itemDataBuilder->addItemData(
                                                    $this->escaper->escapeHtml($category->getName().'%level-6%'.$category->getParentId()),
                                                    $category->getId(),
                                                    $optionsFacetedData[$category->getId()]['count']
                                                );
                                            }
                                        }
                            }
                        }
                    }
                }
        }
    }
    return $this->itemDataBuilder->build();
}
}
7
  • Hi Nitesh, I am using megaplaza laer navigation its already override in this module but i have same issues Commented Jul 24, 2018 at 10:10
  • @ErSarveshVTiwari Did you tried above solution? Is it working for you? Commented Jul 25, 2018 at 14:49
  • sorry bro it did not work for me Commented Aug 8, 2018 at 13:41
  • @ErSarveshVTiwari which Magento version are you using? Commented Aug 9, 2018 at 8:52
  • Hi, I am using Magento 2.1. can you guide me which file I need to override?
    – kiran
    Commented Nov 8, 2018 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.