1

I have to load parent category, sub category and its sub categories. As of now, I can able to load the parent and its sub category but I have no idea how to load its sub category.

Below is the code I have used in template file:

<ul class="clearfix backcolor designmenu">
          <div class="totalmobilelog">
            <div class="lo">
              <a href="" ><b class="clsml"><i class="fa fa-user-plus" aria-hidden="true"></i>Login</b></a>
            </div>
            <div class="si">
              <a href="" ><b class="clsml"><i class="fa fa-registered" aria-hidden="true"></i>Sign Up</b></a>
            </div>
          </div>
          <?php
          $categories = $block->getStoreCategories();
          foreach ($categories as $category):

          ?>
          <li><a href="<?php echo $category->getRequestPath();?>"><?php echo $category->getName(); ?></a>
          <ul>
            <?php
            $subCategories = $block->getCategoryById($category->getId());
            foreach ($subCategories->getChildrenCategories() as $subCategory):
            ?>

            <li><a href="<?php echo $subCategory->getRequestPath();?>"><?php echo $subCategory->getName(); ?></a>
          </li>

          <?php endforeach; ?>
        </ul>
        <?php endforeach; ?>
      </li>
  </ul>

Code in block file:

protected $_categoryHelper;
protected $categoryFactory;
protected $_catalogLayer;
protected $_categoryRepository;


public function __construct(
    \Magento\Catalog\Block\Product\Context $context,     
    \Magento\Catalog\Helper\Category $categoryHelper,        
    \Magento\Catalog\Model\CategoryRepository $categoryRepository,        

    array $data = []
) {

    $this->_categoryHelper = $categoryHelper;   
    $this->_categoryRepository = $categoryRepository;
    parent::__construct(
        $context,          
        $data
    );


}


public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
{
    return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
}

public function getCategoryById($categoryId) 
{
    return $this->_categoryRepository->get($categoryId);

}

Can anyone please help me to load sub categories of sub category?

Code Tried:

<ul class="clearfix backcolor designmenu">
          <div class="totalmobilelog">
            <div class="lo">
              <a href="" ><b class="clsml"><i class="fa fa-user-plus" aria-hidden="true"></i>Login</b></a>
            </div>
            <div class="si">
              <a href="" ><b class="clsml"><i class="fa fa-registered" aria-hidden="true"></i>Sign Up</b></a>
            </div>
          </div>
          <?php
          $categories = $block->getStoreCategories();
          foreach ($categories as $category):

          ?>
          <li><a href="<?php echo $category->getRequestPath();?>"><?php echo $category->getName(); ?></a>
          <ul>
            <?php
            $subCategories = $block->getCategoryById($category->getId());
            foreach ($subCategories->getChildrenCategories() as $subCategory):
              $grandChildrenCategories[] = $subCategory->getChildrenCategories();
            ?>
            <li><a href="<?php echo $subCategory->getRequestPath();?>"><?php echo $subCategory->getName(); ?></a>
              <?php>
              foreach ($grandChildrenCategories as $subsubCategory):

              ?>
              <ul>

                <li><a href=""><?php echo $subsubCategory->getName(); ?></a></li>
              </ul>
            <?php endforeach; ?>
          </li>


          <?php endforeach; ?>
        </ul>
        <?php endforeach; ?>
      </li>
  </ul>

enter image description here

Code used in phtm:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
/**
 * Top menu for store
 *
 * @var $block \Magento\Theme\Block\Html\Topmenu
 */
?>


    <link rel="stylesheet" type="text/css" href="<?php echo $block->getViewFileUrl('css/cart.css')?>">

  <?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
/**
 * Top menu for store
 *
 * @var $block \Magento\Theme\Block\Html\Topmenu
 */
?>

<header class="headers">
  <div class="header-inrs">
    <div class="menu-container">
      <div class="menus contenedor-menu">
        <ul class="clearfix backcolor designmenu">
          <div class="totalmobilelog">
            <div class="lo">
              <a href="" ><b class="clsml"><i class="fa fa-user-plus" aria-hidden="true"></i>Login</b></a>
            </div>
            <div class="si">
              <a href="" ><b class="clsml"><i class="fa fa-registered" aria-hidden="true"></i>Sign Up</b></a>
            </div>
          </div>
          <?php
          $categories = $block->getStoreCategories();
          foreach ($categories as $category):

          ?>
          <li><a href="<?php echo $category->getRequestPath();?>"><?php echo $category->getName(); ?></a>
          <ul class="subc">
            <?php
            $subCategories = $block->getCategoryById($category->getId());
            foreach ($subCategories->getChildrenCategories() as $subCategory):
            ?>

            <li><a href="<?php echo $subCategory->getRequestPath();?>"><?php echo $subCategory->getName(); ?></a>
          </li>

          <?php endforeach; ?>
        </ul>
        <?php echo $block->getSubcategories($subCategories); ?>
        <?php endforeach; ?>
      </li>
  </ul>
</div>
</div>
</div>
</header>

1 Answer 1

3

1.Please add below method on your block class

 public function  getSubcategories($categories) {
    $array ='';
    if($categories->getChildrenCount()>0){
            $array= '<ul class="child">';
            foreach($categories->getChildrenCategories() as $category) {
            $array .= '<li>'.
            '<a href="' .$category->getRequestPath().'">' .
                  $category->getName() . "</a>\n";
            if($category->hasChildren()) {
                $children = $this->getCategoryById($category->getId());
                 $array .=  $this->getSubcategories($children);
                }
             $array .= '</li>';
            }

        $array .= '</ul>';
    }
    return $array;
}

2. in phtml file add below code

         <ul class="main-ul">
                <?php
                $categorys = $block->getStoreCategories(false,false,true);
                  foreach($categorys as $category){ ?>
                    <li><a href="<?php echo $category->getRequestPath();?>"><?php echo $category->getName(); ?>
            <?php if($category->hasChildren()) {
                     $subCategories = $block->getCategoryById($category->getId()); 
    echo $block->getSubcategories($subCategories); 
} ?>
    </a>
               </li>
          <?php } ?>
        </ul>
7
  • Great. It works great. The only thing now I'm facing is the empty dropdown list comes even the parent category has no sub category. Please look at the attached image.
    – MagentoDev
    Commented Sep 26, 2019 at 16:26
  • Can you please help to make invisible when there is no sub category? I know we have to write condition for that but I'm not sure how can I achieve that
    – MagentoDev
    Commented Sep 26, 2019 at 18:09
  • 1
    Please add condition inside block method name public function getSubcategories($categories) { if($categories>hasChildren() ){ and complete after return result Commented Sep 26, 2019 at 18:46
  • I have tried in hiding the ul if there is no sub category but it's not working. Can you please help me?
    – MagentoDev
    Commented Sep 27, 2019 at 13:28
  • can you please share code of your phtm file? Commented Sep 27, 2019 at 13:45

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.