Magento 1.9 Category – How to Get All Children Categories of a Specific Category

categorymagento-1.9

Currently I have this to get children of a category:

$category = Mage::getModel('catalog/category')->load($categoryId);
$childrenIds = $category->getResource()->getChildren($category, true);

However, it will get only active children. Is there a way to get all children of a specific category, including disabled categories?

Best Answer

user76757,You cannot get inactive because of Flat system is enable for category. The setting is enable at admin In order to see this option, go to Magento admin section and click on: System -> Config -> Catalog and Frontend. "Use Flat Catalog Product". make it No then indexing from index management.There fore you can all inactive category.

Magento is use the flat system for product and category,instead of taking the eav data from one table the data is taken from one table.see at http://inchoo.net/magento/flat-tables-in-magento-and-product-collection/

You can the code how magento write it logic. File:app/code/core/Mage/Catalog/Model/Category.php

 protected function _construct()
{
    // If Flat Data enabled then use it but only on frontend
    $flatHelper = Mage::helper('catalog/category_flat');
    if ($flatHelper->isAvailable() && !Mage::app()->getStore()->isAdmin() && $flatHelper->isBuilt(true)
        && !$this->getDisableFlat()
    ) {
        $this->_init('catalog/category_flat');
        $this->_useFlatResource = true;
    } else {
        $this->_init('catalog/category');
    }
}

So you can not get disable categories when flat enable .

Alternative Solution: for getting inactive and active children categories try this

$category->getChildrenCategoriesWithInactive();

See Alan Strom blog

Related Topic