20 Feb 2014

Get list of categories ,subcategories and sub-subcategories in custom script

This tutorial is going to show you one of the ways to list all sub-categories and sub sub categories through custom script for your Magento store.In This script I have shown all subcategories on the basis of main category means Root Catalog's Id.
If you change the $id = 1 then it will display suabcategory upto the first level that looks like below.


If you change the $id = 3 then it will display suabcategory upto the second level that looks like below.
So lets starts the coding part :
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
Mage::app();
$storename = 'default';

$exporter = new XmlCreateCategoryTree();

class XmlCreateCategoryTree
{
    function __construct()
    {
        ini_set('max_execution_time', 14400);
        chdir(MAGENTO);
        umask(0);
        $this->createCategoryTree();
    }

    public function createCategoryTree()
    {
        $id = 3;
        $categories = $this->getCategories($id);
        if($categories[0]['name'] != ''){
            foreach($categories as $category){
                echo $category['name']."\n";
                $sub_categories = $this->getCategories($category['id']);
                if($sub_categories[0]['name'] != ''){
                    foreach($sub_categories as $sub_category){
                        echo '---'.$sub_category['name']."\n";
                        $sub_sub_categories = $this->getCategories($sub_category['id']);
                        if($sub_sub_categories[0]['name'] != ''){
                            foreach($sub_sub_categories as $sub_sub_category){
                                echo "--- ---".$sub_sub_category['name']."\n";
                            }
                        }
                    }
                }
            }
        }
    }
    
    private function getCategories($id){
        $children = Mage::getModel('catalog/category')->getCategories($id);
        $cat = array();
        foreach ($children as $category){
            $cat['name'] = $category->getName();
            $cat['id'] = $category->getId();
            $allCat[] = $cat;
        }
        return $allCat;
    }
}



Or You can get the categories tree by another method means by collecting all ids from catalog model.

$_category = Mage::getModel('catalog/category');
$treeModel = $_category->getTreeModel();
$treeModel->load();

$allIds = $treeModel->getCollection()->getAllIds();

$_categories = array();
if ($allIds)
{
   foreach ($allIds as $id)
   {
       $category->load($id);
       $_categories[$id]['cat_name'] = $category->getName();
       $_categories[$id]['cat_path'] = $category->getPath();
   }
   
   foreach ($allIds as $Id)
   {
       $path = explode('/', $_categories[$Id]['cat_path']);
       $string = '';
       foreach ($path as $pathId)
       {
           $string.= $_categories[$pathId]['cat_name'] . ' >';
           $count++;
       }
       $_string.= ';' . $Id ."\n";
       echo $_string;
   }
}