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?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.