/** * Recursively returns a value / label array of all active categories * @param Mage_Catalog_Model_Category $category * @param String $parentname * @return array */ private function getChildCategories($category, $parentname = '') { //category not active - skip it if (!$category->getIsActive()) { return ''; } //array containing all the categories to return $ret = array(); /* Add the current category to return array * Root categories shouldn't be selected */ if ($category->getLevel() > 1) { $ret[$category->getID()] = $category->getName() . $parentname; } // get all children if (Mage::helper('catalog/category_flat')->isEnabled()) { $children = (array) $category->getChildrenNodes(); $childrenCount = count($children); } else { $children = $category->getChildrenCategories(); $childrenCount = $children->count(); } $hasChildren = $children && $childrenCount; // select active children $activeChildren = array(); foreach ($children as $child) { if ($child->getIsActive()) { $activeChildren[] = $child; } } $activeChildrenCount = count($activeChildren); $hasActiveChildren = $activeChildrenCount > 0; /** * Use recursion to include all children categories too */ foreach ($activeChildren as $child) { $childarray = $this->getChildCategories($child, " / " . $category->getName() . $parentname); foreach ($childarray as $k => $v) { $ret[$k] = $v; } } return $ret; }
/** * Recursively returns a value / label array of all active categories * * @param Mage_Catalog_Model_Category $category * @param String $parentname * @return array */ private function getChildCategories($category, $parentname = '') { //category not active - skip it if (!$category->getIsActive()) { return ''; } //array containing all the categories to return $ret = array(); /* Add the current category to return array * Root categories shouldn't be selected */ if ($category->getLevel() > 1) { $ret[$category->getID()] = ltrim($parentname . " / " . $category->getName(), " / "); } // get all children $children = $category->getChildrenCategories(); $childrenCount = $children->count(); $hasChildren = $children && $childrenCount; // select active children $activeChildren = array(); foreach ($children as $child) { if ($child->getIsActive()) { $activeChildren[] = $child; } } $activeChildrenCount = count($activeChildren); $hasActiveChildren = $activeChildrenCount > 0; /** * Use recursion to include all children categories too */ foreach ($activeChildren as $child) { $childarray = $this->getChildCategories($child, $parentname . " / " . $category->getName()); foreach ($childarray as $k => $v) { $ret[$k] = ltrim($v, " / "); } } return $ret; }