Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 2 of 2
get a list of all categories
 
miked2004
Sr. Member
 
Avatar
Total Posts:  214
Joined:  2007-12-13
Atlanta, Georgia
 

I have also had problems filtering by the Store. Anyone get this working?

 
Magento Community Magento Community
Magento Community
Magento Community
 
rrroulio
Sr. Member
 
Avatar
Total Posts:  88
Joined:  2008-03-26
france moselle
 

if you just want, like me , get all categories with hierarchy + id in order to use in datasheet
create a file “myfile.php” on the root magento folder and put the code below.
All level are marked by *. Use it as separator in excel.

<?php require_once $_SERVER['DOCUMENT_ROOT']."/app/Mage.php";
Mage::app('1');

function 
nodeToArray(Varien_Data_Tree_Node $node)
{
$result 
= array();
$result['category_id'$node->getId();
$result['parent_id'$node->getParentId();
$result['name'$node->getName();
$result['is_active'$node->getIsActive();
$result['position'$node->getPosition();
$result['level'$node->getLevel();
$result['children'= array();

foreach (
$node->getChildren() as $child{
$result[
'children'][] nodeToArray($child);
}

return $result;
}

function load_tree() {

$tree 
Mage::getResourceSingleton('catalog/category_tree')
->
load();

$store 1;
$parentId 1;

$tree Mage::getResourceSingleton('catalog/category_tree')
->
load();

$root $tree->getNodeById($parentId);

if(
$root && $root->getId() == 1{
$root
->setName(Mage::helper('catalog')->__('Root'));
}

$collection 
Mage::getModel('catalog/category')->getCollection()
->
setStoreId($store)
->
addAttributeToSelect('name')
//->addAttributeToSelect('id')
->addAttributeToSelect('is_active');

$tree->addCollectionData($collectiontrue);

return 
nodeToArray($root);

}

function print_tree($tree,$level{
$level 
++;
foreach(
$tree as $item{
echo str_repeat("*"$level).$item['name'].'*'.$item['category_id']."<br>";
print_tree($item['children'],$level);

}
}

$tree 
load_tree();
print_tree($tree['children'],0);
 
Magento Community Magento Community
Magento Community
Magento Community
 
xenn
Jr. Member
 
Total Posts:  16
Joined:  2009-08-11
 

rrroulio - Thanks! Just what I needed

 
Magento Community Magento Community
Magento Community
Magento Community
 
Net Assets
Jr. Member
 
Total Posts:  12
Joined:  2010-08-20
 

rrroulio - you just saved me hours of labor . +1

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
evanjacobs
Jr. Member
 
Total Posts:  4
Joined:  2011-01-22
 

I have modified rrroulio\’s code to return an array (which is much easier to work with) with the category ID as the key as well as the name and children of the category.

require_once "app/Mage.php";
Mage::app('1');

function 
nodeToArray(Varien_Data_Tree_Node $node{
    $result 
= array();
    
$result['category_id'$node->getId();
    
$result['parent_id'$node->getParentId();
    
$result['name'$node->getName();
    
$result['is_active'$node->getIsActive();
    
$result['position'$node->getPosition();
    
$result['level'$node->getLevel();
    
$result['children'= array();
    
    foreach (
$node->getChildren() as $child{
        $result[
'children'][] nodeToArray($child);
    
}
    
    
return $result;
}

function load_tree() {
    $tree 
Mage::getResourceSingleton('catalog/category_tree')->load();
    
    
$store 1;
    
$parentId 1;
    
    
$tree Mage::getResourceSingleton('catalog/category_tree')->load();
    
$root $tree->getNodeById($parentId);
    
    if(
$root && $root->getId() == 1{
        $root
->setName(Mage::helper('catalog')->__('Root'));
    
}
    
    $collection 
Mage::getModel('catalog/category')->getCollection()->setStoreId($store)->addAttributeToSelect('name')->addAttributeToSelect('is_active');
    
$tree->addCollectionData($collectiontrue);
    
    return 
nodeToArray($root);

}

$cats 
= array();
function 
print_tree($tree,&$current_cat{
    
    
foreach($tree as $item{
        $current_cat[$item[
'category_id']] = array();
        
$current_cat[$item['category_id']]['name'$item['name'];
        
        if (
count($item['children']) > 0{
            $current_cat[$item[
'category_id']]['children'= array();
            
print_tree($item['children'], &$current_cat[$item['category_id']]['children');
        
}

            
    
    }
    
return $current_cat;
}

$tree 
load_tree();
$mytree print_tree($tree['children'],&$cats);

print_r($cats);
 
Magento Community Magento Community
Magento Community
Magento Community
 
ahmadTheGeek
Jr. Member
 
Total Posts:  3
Joined:  2008-07-17
 

$_categories $this->getCurrentCategory()->getAllChildren(true);
That’s the easiest way
 
Magento Community Magento Community
Magento Community
Magento Community
 
ch_pavankumar
Jr. Member
 
Total Posts:  12
Joined:  2012-12-14
 

@rrroulio i did as what u have explained. but after copy/paste this code, it returns blank screen. I have pasted the fileunder magento root only. I have a doubt that how this file connects to database.. At last finally I want to get the list of categories in JSON format. No need to display at frontend.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jayanta1988
Jr. Member
 
Avatar
Total Posts:  17
Joined:  2012-09-19
 

$rootCatId Mage::app()->getStore()->getRootCategoryId();
     
getTreeCategories($rootCatIdfalse);
    function 
getTreeCategories($parentId$file$isChild){
         $allCats 
Mage::getModel('catalog/category')->getCollection()
                    ->
addAttributeToSelect('*')
                    ->
addAttributeToFilter('is_active','1')
                    
//->addAttributeToFilter('include_in_menu','1')
                    
->addAttributeToFilter('parent_id',array('eq' => $parentId))
                    ->
addAttributeToSort('position''asc');
        
        foreach(
$allCats as $category)
        
{
            
echo $string $category->getId() . '=>' .$category->getName() . "<br/>";
            
            
$subcats $category->getChildren();
            if(
$subcats != ''){
                getTreeCategories
($category->getId(), true);
            
}
        }
    }

also see this http://justonestepsolution.blogspot.in/2012/09/get-all-categories-in-magento.html

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 2 of 2