Magento 2 Display Sub Category of parent?

My code to get the category list for the main category is:

public function __construct(
        Template\Context $context, 
        \Magento\Catalog\Model\Layer\Resolver $layerResolver, 
        \Magento\Framework\Registry $registry, 
        \Magento\Catalog\Helper\Category $categoryHelper, 
        \Magento\Catalog\Model\CategoryFactory  $categoryFactory,
        array $data = array()) 
    {
        parent::__construct($context, $layerResolver, $registry, $categoryHelper,$data);
        $this->_categoryFactory = $categoryFactory;
        $this->_collectionFactory = $collectionFactory;
     }

    public function getCategoryList()
    {
      $_category  = $this->getCurrentCategory();
      $collection = $this->_categoryFactory->create()->getCollection()->addAttributeToSelect('*')
              ->addAttributeToFilter('is_active', 1)
              ->setOrder('position', 'ASC')
              ->addIdFilter($_category->getChildren());
      return $collection;

    }

For reference you can start with core files See vendor/magento/module-catalog/Block/Navigation.php

getCurrentChildCategories() function:

$categories = $this->_catalogLayer->getCurrentCategory()->getChildrenCategories();

Where getCurrentCategory simply fetches current category from registry just like you did. So getChildrenCategories should work for you.

Also, more functions related to child/sub-categories are available in this file. vendor/magento/module-catalog/Model/Category.php . So if you get category object you can fetch sub-category data from it by using any of the functions defined in this file, as per your requirement.

Hope this helps!!


In your theme add the following to the appropriate container in your app/design/frontend/YourNamespace/YourTheme/layout/catalog_category_view.xml layout file.

<block class="Magento\Catalog\Block\Navigation"
           name="catalog.subnav"
           after="-"
           template="Magento_Catalog::category/category-subnav.phtml"/>

Then create a new file in app/design/frontend/YourNamespace/YourTheme/Magento_Catalog/templates/category/category-subnav.phtml and add the following.

<?php if (!$block->getCategory()) {
    return;
} ?>
<?php $_categories = $block->getCategory()->getParentCategory()->getChildrenCategories(); ?>
<?php $_count = is_array($_categories) ? count($_categories) : $_categories->count(); ?>
<?php if ($_count): ?>
    <div class="block subnav">
        <div class="content">
            <ol class="items">
                <?php foreach ($_categories as $_category): ?>
                    <?php if ($_category->getIsActive()): ?>
                        <li class="item">
                            <a href="<?php /* @escapeNotVerified */ echo $block->getCategoryUrl($_category) ?>"<?php if ($block->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $block->escapeHtml($_category->getName()) ?></a>
                        </li>
                    <?php endif; ?>
                <?php endforeach ?>
            </ol>
        </div>
    </div>
<?php endif; ?>

This should give you a list containing the current category and its siblings/child.

Tags:

Magento2