1

I can get a list of the children categories for a specific category with the following code:

public function displaycats($data, $begin,$end, $catid){
    $currentCat = Mage::getModel('catalog/category')->load($data[0]);
    $children = explode(",",$currentCat->getChildren());

    foreach ($children as $child) {
    $cot++;
    $subCat = Mage::getModel('catalog/category')->load($child);

        if ($cot >= $begin && $cot <= $end){
            echo '<a href="'.$subCat->getUrl().'?stockable=786">'.$subCat->getName()."</a><br>";
        }
    }
    return;
}

The problem is that the list is not ordered the same as Magento has the categories ordered in the admin area. Can anyone help me out?

4 Answers 4

7

I like @Joe Constant's answer, but when I tried it, I was having trouble only getting the first level of child categories, even with $recursionLevel set to 0. As a workaround, I wrote this function (place it in (local version of) whichever block you need the sorted child collection):

public function getChildrenCollection($parentId=false, $sort='ASC', $attribute='position') 
{
  if (empty($parentId) || !is_numeric($parentId)) return false;
  $childrenArray = explode(',',Mage::getModel('catalog/category')->load($parentId)->getChildren());
  // remove parent id from array in case it gets returned
  if ($key = array_search($parentId, $childrenArray)) {
    unset($childrenArray[$key]);
  } 
  $collection = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToFilter('entity_id', array('in' => $childrenArray))
    ->addAttributeToSelect('*');

  if (!empty($sort)) {
    return $collection->setOrder($attribute, $sort);
  }
  return $collection;           
}

Then you can iterate through the collection and do whatever you need like this:

foreach (getChildrenCollection($parentCategoryId) as $child) {
  Zend_Debug::dump($child->getData());
}

Hope that helps.

2

Look at getCategories instead. It has a sorted parameter as well as an option to return as a collection object removing the need to load each category from the database again.

/**
 * Retrieve categories
 *
 * @param integer $parent
 * @param integer $recursionLevel
 * @param boolean|string $sorted
 * @param boolean $asCollection
 * @param boolean $toLoad
 * @return Varien_Data_Tree_Node_Collection|Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection
 */
public function getCategories($parent, $recursionLevel = 0, $sorted=false, $asCollection=false, $toLoad=true)

`

2
    $helper     = Mage::helper('catalog/category');
    $_categories     = $helper->getStoreCategories('path', true, FALSE);

If you want to display the category in the following format

category 1

--- sub category 1a

------ sub category 1aa

--- sub category 1b

category 2

---sub category 2a

---sub category 2b

    foreach($_categories as $category){
        $level = $category['level'] - 2;
        $pad = str_repeat("---", ($level > 0) ? $level : 0);

        echo $pad . ' ' . $cat['name']);
        echo '<br/>';
    }

    return $categories;
2

I just wrote something like this to get list of categories (by parentId) sorted like in admin:

$subCats = array();
$children = Mage::getModel('catalog/category')->getCategories($parentId, 1, true, true);
foreach ($children as $category)
{
    $subCats[$category->getPosition()] = $category->getId();
}
ksort($subCats);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.