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