ylliX - Online Advertising Network How to get Magento categories with full paths - Yet Another Programmer's Blog

How to get Magento categories with full paths

Maybe someone will need it, so here we go with code:

echo "<pre>";
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();

$ids = $tree->getCollection()->getAllIds();
$categories = array();
if ($ids)
{
    foreach ($ids as $id)
    {
        $category->load($id);
        $categories[$id]['name'] = $category->getName();
        $categories[$id]['path'] = $category->getPath();
    }
    foreach ($ids as $id)
    {
        $path = explode('/', $categories[$id]['path']);
        $string = '';
        foreach ($path as $pathId)
        {
            $string.= $categories[$pathId]['name'] . ' > ';
            $cnt++;
        }
        $string.= ';' . $id . "\n";

        echo $string;
    }
}

 

the snippet above will give you such output:

root category 1 ; 1
root category 1 > subcategory 1 ; 2
root category 1 > subcategory 1 > subsubcategory 1 ; 3
root category 1 > subcategory 1 > subsubcategory 2 ; 4
root category 1 > subcategory 2 ; 5
root category 1 > subcategory 2 > subsubcategory 1 ; 6

9 thoughts on “How to get Magento categories with full paths

  1. Hello;

    i need current category tree menu can i know how can i do that?

     

    Thanks in advance

  2. \n didn’t worked for me, so I used PHP_EOL. Also I added some code from another php-Script to save the category-tree in a .csv file (path=var/export/catwithid.csv):

    <?php 
    
    define('MAGENTO', realpath(dirname(__FILE__)));
    require_once MAGENTO . '/app/Mage.php';
    Mage::app();
    
    $category = Mage::getModel('catalog/category');
    $tree = $category->getTreeModel();
    $tree->load();
     
    $ids = $tree->getCollection()->getAllIds();
    $categories = array();
    if ($ids)
    {
    	$file = "var/export/catwithid.csv";
    	file_put_contents($file,"catId, catPfad");
        foreach ($ids as $id)
        {
            $category->load($id);
            $categories[$id]['name'] = $category->getName();
            $categories[$id]['path'] = $category->getPath();
        }
        foreach ($ids as $id)
        {
            $path = explode('/', $categories[$id]['path']);
            $string = '' . $id . ',';
            foreach ($path as $pathId)
            {
                $string.= $categories[$pathId]['name'] . ' > ';
    			
                $cnt++;
            }
            $string = $string . PHP_EOL;
    		file_put_contents($file,$string,FILE_APPEND);
     
            echo $string;
        }
    }
    ?>

     

  3. Thanks for neat snippet.  For folks who wants to fetch all categories (not just current store categories), make sure your script is executed in admin store. Something like the code below will help. When script is ran as admin-store categories are read from ‘catalog_category_entity’  table. Without admin, categories are fetched from catalog_category_flat_store_x table.

    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    

Leave a Reply