Try the Demo

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?

 Signature 

~ Mike D
http://www.sharpdotinc.com
http://www.sharpdotinc.com/mdost

 
Magento Community Magento Community
Magento Community
Magento Community
 
rrroulio
Sr. Member
 
Avatar
Total Posts:  82
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:  7
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:  2
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
Magento Community
Magento Community
    Back to top
Page 2 of 2