I am getting the subcategories of a main category like this(this file is in root folder)

use \Magento\Framework\App\Bootstrap;
$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$subcategory_1 = $objectManager->create('Magento\Catalog\Model\Category')->load(4);//My category Id
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

From this collection how can i check is there any subcategory is available or not?

1 Answer 1


You can try something like this

$subcategory_1 = $objectManager->create('Magento\Catalog\Model\Category')->load(4);
$children = $subcategory_1->getAllChildren(true);
  // Sub Category exists
  • Thanks.This will return both exist for all main category Ids
    – Mujahidh
    Dec 6, 2017 at 10:39
  • The will return child category IDs for category ID 4. If its empty there are no sub categories for ID 4.
    – Priyank
    Dec 6, 2017 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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