Thursday, 5 November 2015

Get sub-categories of a specific parent category

get sub categories of specific category 
<?php 
    $children = Mage::getModel('catalog/category')->getCategories(10);
        foreach ($children as $category) {
            echo $category->getName();
        }
?>

------------------------------------------------------------------------------ 
get child category of every current category 
 <?php 
    $layer = Mage::getSingleton('catalog/layer');
    $_category = $layer->getCurrentCategory();
    $currentCategoryId= $_category->getId();
    $children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
    foreach ($children as $category)
    {
          echo $category->getName(); // will return category name 
          echo $category->getRequestPath(); // will return category URL
    }
?>
------------------------------------------------------------------------------
<?php 
 $parentCategoryId = 107;
 $cat = Mage::getModel('catalog/category')->load($parentCategoryId);
 $subcats = $cat->getChildren();
 
 // 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>';
   }
 }

?>
------------------------------------------------------------------------------
<?php 
 $children = Mage::getModel('catalog/category')->load('10')->getChildrenCategories();
 foreach ($children as $category):
  $category = Mage::getModel('catalog/category')->load($category->getId());
  echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
 endforeach;
?>
-------------------------------------------------------------------------------
<?php 
$root = Mage::getModel('catalog/category')->load(13); 
$subCat = explode(',',$root->getChildren()); 

$collection  = $root
             ->getCollection()
             ->addAttributeToSelect("*")
             ->addFieldToFilter("entity_id", array("in", $subCat) );

foreach($collection as $catname){
echo $catname->getName();
}
?> 

No comments:

Post a Comment

about new GraphQL freature in Magento2

GraphQL: GraphQL is a data query language developed internally by Facebook in 2012 before being publicly released in 2015. Magento impleme...