Magento 2 get child categories


Magento 2 get child categories of specific category – We can get all sub categories of the specific category in Magento2. Sub categories can be get from parent category. Here in this article how we can get child categories by parent category id,we will also cover how to get current category’s child/sub categories in Magento 2.


Magento 2 get child categories Example

You can get child categories simply as below. Let us go one by one-

Get Sub Categories By Id

You can get sub categories simply as below-

Get Sub Categories Example:

$categoryId = 5;
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
foreach ($category->getChildCategories() as $child) {
    echo $child->getId();
}

Get Child Categories From Current Category

You can get child categories from current category simply as below-

Get Sub Categories Example:

 /* $block \Magento\Catalog\Block\Category\View */
foreach ($block->getCurrentCategory()->getChildCategories() as $child) {
    echo $child->getId();
}

So the above example will give you the child categories of current category.


Advertisements

Add Comment

📖 Read More