Magento – How to do a list with all categories and subcategories, from a given main-category

categorycollection;

I like to do a list with the name of all subcategories and sub subcategories from a giving main category.

I am really a bit new in Magento. But what I want to do, a list from all subcategories (name and url, as href) from the main category "Brands":

https://mag.outdoorequipped.com/

without the "All Brands" category but with those subcategories from "All Brands".

Any suggestions or link I can find some solution?

Best Answer

Try this.

Assume the ID of category "All Brands" is 3

    $parentCategoryId = 3;
    $childCategories = Mage::getModel('catalog/category')->getCategories($parentCategoryId);

  foreach ($childCategories as $child) {
        echo 'Name : ' . $child->getName() . '<br>'; // will print the sub category name
        echo 'Url : ' . $child->getRequestPath() . '<br><hr>'; // will print the URL
    }

NOTE: If you want to get more info about the sub category use below lines inside above foreach() loop.

$subCatData = $child->getData();

It will get an array of sub category data.

EDIT:

If you are in the category page you can get the current category ID by using below line of code. (instead of hard-coding the category ID use this way)

$ccurrentCategoryId = Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId();

So now the code would be as follows in your scenario.

 $parentCategoryId = Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId();
        $childCategories = Mage::getModel('catalog/category')->getCategories($parentCategoryId);

      foreach ($childCategories as $child) {
            echo 'Name : ' . $child->getName() . '<br>'; // will print the sub category name
            echo 'Url : ' . $child->getRequestPath() . '<br><hr>'; // will print the URL
        }
Related Topic