12
votes

Trying to get child of a specific category which is active. Please help. I am having trouble doing it. I'm currently able to show them all but not specifically. Would appreciate any help.

$category = Mage::getModel('catalog/category')->load(2);
$category->getChildCategories();
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
6
Get all the child category details, rakeshjesadiya.com/…Rakesh Jesadiya

6 Answers

36
votes

here is code to load active category

/* Load category by id*/
$cat = Mage::getModel('catalog/category')->load($id);


/*Returns comma separated ids*/
$subcats = $cat->getChildren();

//Print out categories string
#print_r($subcats);

foreach(explode(',',$subcats) as $subCatid)
{
  $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive())
  {
    $caturl     = $_category->getURL();
    $catname     = $_category->getName();
    if($_category->getImageUrl())
    {
      $catimg     = $_category->getImageUrl();
    }
    echo '<h2><a href="'.$caturl.'" title="View the products for this category"><img src="'.$catimg.'" alt="" />'.$catname.'</a></h2>';
  }
}
?>

hope this is sure help you.

13
votes

As mentioned by mhaupt, it is faster to load a collection rather than each category in a loop. But, as far as I am concerned, there is no need to manually load the child categories. Basically this is what $category->getChildrenCategories() already does.

There is also a filter to get active categories only. Just call addIsActiveFilter() on the collection.

a.) Load active child categories via getChildren()

// 1. Get a list of all child category ids (e.g "12,23,11,42")
$subcategoryIds = $category->getChildren();

// 2. Create collection
$categoryCollection = Mage::getModel('catalog/category')->getCollection();

// 3. Add all attributes to select, otherwise you can not 
//    access things like $cat->getName() etc.
$categoryCollection->addAttributeToSelect('*');

// 4. Filter by ids
$categoryCollection->addIdFilter($subcategoryIds);

// 5. Add filter to collection to get active categories only
$categoryCollection->addIsActiveFilter();

b.) Load active child categories with getChildrenCategories()

// 1. Load collection
$categoryCollection= $category->getChildrenCategories();

// 2. Add filter to collection to get active categories only
$categoryCollection->addIsActiveFilter();

The collection will be loaded form the database as soon as it is accessed. If the collection is not loaded and $subcategories->count() is called only a "SELECT count(*)" will be fired against the database (in contrast to count($subcategories) which will force the collection to load itself).

Iterating the collection

foreach($categoryCollection as $category) {
    echo $category->getName();
}

If you add more filters to the collection after accessing it, the collection will not load itself again automatically. To apply changes to the collection, just call $categoryCollection->load() to reload the collection from the database.

5
votes

Those who are saying to use getAllChildren() instead of getChildren() are simply wrong. Both methods return the exact same thing, with one difference, getAllChildren(true) will return an array instead of a comma delimited string. getAllChildren($bool asArray) defaults to false. My point being that either way you're going to have to use

Mage::getModel('catalog/category')->load($catId);

inside of a loop unless you use the function below.

private function fetchCatsById($onlyThese)
{
    $cats = Mage::getModel('catalog/category')
                ->getCollection(true)
                ->addAttributeToSelect('*')
                ->addIdFilter($onlyThese)
                ->addAttributeToFilter('level','2')
                ->addIsActiveFilter();

    return $cats;
}

$cats = $this->fetchCatsById($onlyThese);
2
votes

The one answer liyakat wrote, should not be used in professional shops, because it raises a performance issue, because of the multiple n time loads of the category object, rather use the collection of categories for that, get all children

$cat->getAllChildren()

, then limit the category collection by the needed category ids like

$coll->addIdFilter($idFilter);

then you won't have to load n times against the database.

Please do keep in mind that loads within loops are one of the most often used bad code examples in any Magento projects and to avoid them!

1
votes

Hello you will see below code

$category_model = Mage::getModel('catalog/category'); 
  $_category = $category_model->load(13); 
  $all_child_categories = $category_model->getResource()->getAllChildren($_category);
  print_r($all_child_categories);
1
votes