How To Get Sub Categories in Magento ?`

15,028

Do this instead :

Mage::getModel('catalog/category')->load('23')->getChildrenCategories();

and iterate over the result.

and that's how i found it out:

$object = Mage::getModel('catalog/category'); 
print_r(get_class_methods($object));
print_r($object->load('23')->getChildrenCategories()->toArray());
Share:
15,028
ScoRpion
Author by

ScoRpion

I Work On PHP Symfony2 Magento Zend Fusebox Python OpenERP JavaScript JQuery Ajax ( JQuery And Prototype ) And I am passionate about sketching my new ideas into programming Applications.

Updated on June 05, 2022

Comments

  • ScoRpion
    ScoRpion almost 2 years

    I am playing with magento's home page where I have created a tab Which Shows all the categories including Root, categories, and Sub Categories (In One Tab). Now I want to Show Only Main Categories( Whose Parent Is Root) in main Tab And under Each Category I want to List Their respective Sub Categories. I wrote The following Code to achieve a part of this,

    MODEL CLASS

    public function getsubCategory($parent)
    {
    
        $subcategoryCollection = Mage::getModel('catalog/category')
        ->getCollection()
        ->addAttributeToFilter('parent_id', $parent);
           return $subcategoryCollection;
    

    BLOCK CLASS

    protected function b4Html_subcategory($parent)
    {
        $catModel = Mage::getModel('Pragtech_Sweet/category');
        $mysubCategory = $catModel->getsubCategory($parent);
        $this->mysubCategory = $myCategory; 
        return $mysubCategory;
    }
    

    TEMPLATE FILE

    $obj = new Pragtech_Sweet_Block_Category();
    $collection = $obj->b4Html();
    foreach ($collection as $category)
        {
        $name = $category->getName();
        $parent = $category->getParent_id();
    
        foreach ($obj->b4Html_subcategory($parent) as $subcategory)
        {   
           $subname = $subcategory->getName();
           //Here Will Go Ther Code For Sub Categories
    
        }
    

    but it doesn't work.. I am unable to understand where I am doing wrong... Can anyone help me out