Magento 1.9 - get all subcategories of parent category

1) Parent category

1
2
3
4
//Parent category
$parentCategoryId = 107;
$cat = Mage::getModel('catalog/category')->load($parentCategoryId);
$subcats = $cat->getChildren();

2) Get 1 Level sub category of Parent category.

1
2
3
4
5
6
7
8
//Get 1 Level sub category of Parent category
foreach(explode(',',$subcats) as $subCatid){
  $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive()) {
    echo '<ul><a href="'.$_category->getURL().'" title="View the products for the " '.$_category-="">getName().'" category">'.$_category->getName().'</a>';
   echo '</ul>';
  }
}

3) Get 2 Level sub category of Parent sub category.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Get 2 Level sub category of Parent sub category
 
foreach(explode(',',$subcats) as $subCatid)
{
  $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive()) {
    echo '<ul><a href="'.$_category->getURL().'" title="View the products for the " '.$_category-="">getName().'" category">'.$_category->getName().'</a>';
    $sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
    $sub_subcats = $sub_cat->getChildren();
    foreach(explode(',',$sub_subcats) as $sub_subCatid){
          $_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
          if($_sub_category->getIsActive()) {
              echo '<li class="sub_cat"><a href="'.$_sub_category->getURL().'" title="View the products for the " '.$_sub_category-="">getName().'" category">'.$_sub_category->getName().'</a></li>';
 
        }
     }
     echo '</ul>';
  }
}

4) Get 3 Level sub sub category of Parent sub sub category

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Get 3 Level sub sub category of Parent sub sub category
 
foreach(explode(',',$subcats) as $subCatid)
{
  $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive()) {
    echo '<ul><a href="'.$_category->getURL().'" title="View the products for the " '.$_category-="">getName().'" category">'.$_category->getName().'</a>';
    $sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
    $sub_subcats = $sub_cat->getChildren();
    foreach(explode(',',$sub_subcats) as $sub_subCatid)
    {
          $_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
          if($_sub_category->getIsActive()) {
              echo '<li class="sub_cat"><a href="'.$_sub_category->getURL().'" title="View the products for the " '.$_sub_category-="">getName().'" category">'.$_sub_category->getName().'</a></li>';
              $sub_sub_cat = Mage::getModel('catalog/category')->load($sub_subCatid);
              $sub_sub_subcats = $sub_sub_cat->getChildren();
              foreach(explode(',',$sub_sub_subcats) as $sub_sub_subCatid)
              {
                $_sub_sub_category = Mage::getModel('catalog/category')->load($sub_sub_subCatid);
                if($_sub_sub_category->getIsActive()) {
                    echo '<li class="sub_cat"><a href="'.$_sub_sub_category->getURL().'" title="View the products for the " '.$_sub_sub_category-="">getName().'" category">'.$_sub_sub_category->getName().'</a></li>';
                }
              }
           }
     }
     echo '</ul>';
  }
}
1
2
3
4
5
6
7
8
9
10
11
// $sub_subCatid this is sub category id . means if you want 4 level than it will take 3 level subcategory Id.
 
$sub_sub_cat = Mage::getModel('catalog/category')->load($sub_subCatid);
             $sub_sub_subcats = $sub_sub_cat->getChildren();
             foreach(explode(',',$sub_sub_subcats) as $sub_sub_subCatid)
             {
               $_sub_sub_category = Mage::getModel('catalog/category')->load($sub_sub_subCatid);
               if($_sub_sub_category->getIsActive()) {
                   echo '<li class="sub_cat"><a href="'.$_sub_sub_category->getURL().'" title="View the products for the " '.$_sub_sub_category-="">getName().'" category">'.$_sub_sub_category->getName().'</a></li>';
               }
             }

No comments:

Post a Comment