0

I am using Magento 2 and I want to display 5 level of categories.

enter image description here

(1)Shop->(2)shop->(3)audio->(4)Test->(5)test1

I tried below code but 5th level of categories is not display accurate :

public function simpletMenu($category)
    {
        $categoryHelper = $this->getCategoryHelper();
        $catHtml = '';
        // 2nd Level Category
        if ($childrenCategories = $this->getChildCategories($category)) {
            $catHtml .= '<ul class="rootmenu-submenu">';
            foreach ($childrenCategories as $childCategory) {
                $collection_sub = $this->getChildCategories($childCategory);
                if (count($collection_sub)) {
                    $arrow = '<span class="cat-arrow"></span>';
                } else { 
                    $arrow = '';
                }
                $catHtml .= '<li><a href="'.$categoryHelper->getCategoryUrl($childCategory).'">'.$childCategory->getName().$arrow.'</a>';

                    // 3rd Level Category
                    if (count($collection_sub)) {
                        $catHtml .= '<ul class="rootmenu-submenu-sub">';
                            foreach ($collection_sub as $childCategory2) {
                                $collection_sub_sub = $this->getChildCategories($childCategory2);
                                if (count($collection_sub_sub)) {
                                    $arrow = '<span class="cat-arrow"></span>';
                                } else {
                                    $arrow = '';
                                }
                                $catHtml .= '<li><a href="'.$categoryHelper->getCategoryUrl($childCategory2).'">'.$childCategory2->getName().$arrow.'</a>';

                                // 4th Level Category
                                if (count($collection_sub_sub)) {
                                    $catHtml .= ' <ul class="rootmenu-submenu-sub-sub">';
                                        foreach ($collection_sub_sub as $childCategory3) {
                                            $collection_sub_sub_sub = $this->getChildCategories($childCategory3);
                                        if (count($collection_sub_sub_sub)) {
                                            $arrow = '<span class="cat-arrow"></span>';
                                        } else {
                                            $arrow = '';
                                        }
                                            $catHtml .= '<li><a href="'.$categoryHelper->getCategoryUrl($childCategory3).'">'.$childCategory3->getName().$arrow.'</a>';

                                    // 4th Level Category
                                    if (count($collection_sub_sub_sub)) {
                                        $catHtml .= ' <ul class="rootmenu-submenu-sub-sub-sub">';
                                            foreach ($collection_sub_sub_sub as $childCategory4) {
                                                $catHtml .= '<li><a href="'.$categoryHelper->getCategoryUrl($childCategory4).'">'.$childCategory4->getName().'</a></li>';
                                             }
                                        $catHtml .= '</ul>';
                                    }   
                                            $catHtml .= '</li>';
                                         }
                                    $catHtml .= '</ul>';
                                }
                                $catHtml .= '</li>';
                             }
                        $catHtml .= '</ul>';
                    }
                $catHtml .= '</li>';
            }
            $catHtml .= '</ul>';
        }
        return $catHtml;
    }
0

You have to use a category collection in your block, then you filter per level addLevelFilter() after that you get that function in your phtml like this:

$block->getCategoryLevelCollection($level)

public function getCategoryLevelCollection($level)
{
    $collection = $this->_categoryCollection->create()
        ->addAttributeToSelect('*')
        ->setStore($this->_storeManager->getStore())
        ->addAttributeToFilter('is_active','1')
        ->addLevelFilter($level);
    return $collection;
}

Your Answer

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

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