0

I want all category in magento like under given structure.

parent1
   par2
     child1
   par3
     child2
     child3
     child4
   par4
parent2
   par5
     child5
   par6
     child6
parent3 
parent4

Can any one help?

  • There's no easy way to get the entire category tree, what is it you are trying to do exactly? – Jonathan Hussey Nov 24 '14 at 11:00
  • getStoreCategories() in Mage/catalog/block/Navigation returns top categories, then use getChildrenCategories() to get category children. – Manikandan Arunachalam Nov 24 '14 at 11:08
1

You can use this script to Display top level categories and subcategories

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                <?php $_subcategories = $_category->getChildrenCategories() ?>
                <?php if (count($_subcategories) > 0): ?>
                    <ul>
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <li>
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                    <?php echo $_subcategory->getName() ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

If you want to get level 3 or leave 4 categories you need to repeat this :

<?php $_subcategories = $_category->getChildrenCategories() ?>
                    <?php if (count($_subcategories) > 0): ?>
                        <ul>
                            <?php foreach($_subcategories as $_subcategory): ?>
                                <li>
                                    <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                        <?php echo $_subcategory->getName() ?>
                                    </a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
0

Based on Ahmed's answer, here is a basic recursive method to list subcategories starting at category ID 1 (Default Category).

function showSubCategories($categoryId)
{
    if ($categoryId) {
        $category = Mage::getModel('catalog/category')->load($categoryId);
        $subCats = $category->getChildrenCategories();
        if (count($subCats) > 0) {
            echo '<ul>';
            foreach ($subCats as $subCat) {
                echo "<li><a href=\"{$subCat->getUrl()}\">{$subCat->getName()}</a>";
                showSubCategories($subCat->getId());
                echo '</li>';
            }
            echo '</ul>';
        }
    }
}

showSubCategories(1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.