1

I am using WeltPixel layered navigation Free extension. In category filter, third level categories was not showing in filter navigation by default. I added some code to get third level categories.

protected function _getItemsData()
    {
        if (!$this->_moduleHelper->isEnabled()) {
            return parent::_getItemsData();
        }

        /** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $productCollection */
        $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        $productCollection = $this->getLayer()->getProductCollection();

        if ($this->_isFilter) {
            $productCollection = $productCollection->getCollectionClone()
                ->removeAttributeSearch('category_ids');
        }

        $optionsFacetedData = $productCollection->getFacetedData('category');
        $category           = $this->dataProvider->getCategory();
        $categories         = $category->getChildrenCategories();

        $collectionSize = $productCollection->getSize();

        if ($category->getIsActive()) {
            foreach ($categories as $category) {
                $count = isset($optionsFacetedData[$category->getId()]) ? $optionsFacetedData[$category->getId()]['count'] : 0;
                if ($category->getIsActive()
                    && $this->_layerFilter->isOptionReducesResults($this, $count, $collectionSize)
                ) {
                 $this->itemDataBuilder->addItemData(
                        $this->escaper->escapeHtml($category->getName()),
                        $category->getId(),
                        $count
                    );
                }
                // added for child categories
             $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()),
                            $category->getId(),
                            $optionsFacetedData[$category->getId()]['count']
                        );
                    }
                } //
               // end child categories code
        }
    }
return $this->itemDataBuilder->build();
    }
}

Child categories are now showing, but the issue is all parent & child categories are showing in single ol li list. Screenshots

enter image description here

Category List HTML

enter image description here

How to identify Parent and child categories using above code?

  • I have same issue. Do you fix it? – David Duong Jul 31 at 10:54
  • No. I am using another layered navigation extension. – Shoaib Saleem Jul 31 at 11:44
  • Ok , Thank you! – David Duong Jul 31 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.