0

In Magento 2 I want to call the grandchildren of the parent category.

CURRENT CODE

<?php   
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get('Magento\Framework\App\Action\Context')->getRequest();
//echo $request->getFullActionName();
if($request->getFullActionName() === "catalog_category_view"){ 
?>

<?php    
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
 /*echo $category->getName();*/
 $SubCategories = $category->getChildrenCategories();
?>

Can anyone please suggest an update to the above to call the grandchildren rather than children?

YorkieMagento
  • 1,235
  • 4
  • 31
  • 61

1 Answers1

0

Out of the box it's not possible via a direct method but you can do it with a loop by adding the following after your code:

$grandChildrenCategories = array();
foreach ($SubCategories as $subCategory) {
    $grandChildrenCategories[] = $subCategory->getChildrenCategories();
}

Your $grandChildrenCategories variable will be an array of category collection.

Also, please try to avoid using the object manager directly

Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352