Get Three Level Category and Subcategories through Category id in Root file Magento2


Create a File in Root of Magento2

<?php


 use Magento\Framework\App\Bootstrap;
 require __DIR__ . '/app/bootstrap.php';
 $params = $_SERVER;
 $bootstrap = Bootstrap::create(BP, $params);
 $obj = $bootstrap->getObjectManager();
 $state = $obj->get('Magento\Framework\App\State');
 $state->setAreaCode('frontend');


 
 
    /*$params = (array) json_decode(file_get_contents('php://input'), TRUE);
    print_r($params);                            ////////// Get Data From Postman Api/////////

    $catid=$params['caterory'];
    echo "string+++++".$catid;*/
     
    /*echo"<pre>++";
    print_r($_GET['caterory']);*/                      ////  Get Data Through URL////////

    $catid=$_GET['cateroryid'];

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
     
  
    $subCategory = $objectManager->create('Magento\Catalog\Model\Category')->load($catid);  //Parent Category ID

    $subcategories = $subCategory->getChildrenCategories();
    //exit();




      //Print suncategory Ids
    
    foreach($subcategories as $subcategorie) {
        $catsubid=$subcategorie->getId().'<br>';
       echo "==============>>" .$subcategorie->getName().'<br>';  
        if($subcategorie->hasChildren()) {
        $childCategoryObj = $objectManager->create('Magento\Catalog\Model\Category')->load($subcategorie->getId());
        $childSubcategories = $childCategoryObj->getChildrenCategories();
        foreach($childSubcategories as $childSubcategorie) {
            //echo"*******". $childSubcategorie->getId();
         echo "*******". $childSubcategorie->getName().'<br>';
           
           if($childSubcategorie->hasChildren()) {
        $childCategoryObj1 = $objectManager->create('Magento\Catalog\Model\Category')->load($childSubcategorie->getId());
        $childSubcategories1 = $childCategoryObj1->getChildrenCategories();
        foreach($childSubcategories1 as $childSubcategorie1) {
         echo"+++++++>>". $childSubcategorie1->getId();
         echo   $childSubcategorie1->getName().'<br>';

           if($childSubcategorie1->hasChildren()) {
        $childCategoryObj2 = $objectManager->create('Magento\Catalog\Model\Category')->load($childSubcategorie1->getId());
        $childSubcategories2 = $childCategoryObj2->getChildrenCategories();
        foreach($childSubcategories2 as $childSubcategorie2) {
         //echo"===>>". $childSubcategorie2->getId();
         echo ">>>>>>". $childSubcategorie2->getName().'<br>';

       
            
        }
     }    

            
        }
     }    



        }
     }    
   

}

 ?>

Comments

Popular posts from this blog

Get Swatch Text Product id of Configurable Product in Detail Page through Jquery Magento2

Getting product Image in PHTML in Magento2

Get Customer Address with customer ID programmatically Magento2