1

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?

2 Answers 2

2

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
        }
4
0

To get a collection of subcategories of a given category $parentCategory, use:

$subcategoryCollection = Mage::getModel('catalog/category')
    ->getCollection()
    ->addFieldToFilter('level', $parentCategory->getLevel() + 1) // remove this to go deeper
    ->addFieldToFilter('path', ['like' => $parentCategory->getData('path') . '%']);

(as described here: https://stackoverflow.com/q/5564647/664108)

As noted in the code comment above, you can use the level filter to specify how deep you want to recurse into subcategories. To get subcategories and sub-sub-categories but not sub-sub-subcategories, use this:

->addFieldToFilter('level', ['lte' => $parentCategory->getLevel() + 2])

(lte stands for less than or equal)

If you want to show the categories sorted by parent like this

1
1.1
1.1.1
1.1.2
1.2 2
2.1

add this line:

$subcategoryCollection->setOrder('path', Varien_Data_Collection_Db::SORT_ORDER_ASC);

Depending on what data you need to display, select the attributes to select:

$subcategoryCollection->addAttributeToSelect(['name', 'description', 'image']);

You can always select all attributes with addAttributeToSelect('*') instead, but I don't recommend it because it results in unneccessary queries to the EAV tables.

Then you can foreach over $subcategoryCollection and display the subcategories however you wish.

Example template:

<?php foreach ($subcategoryCollection as $category): ?>
    <a href="<?php echo $category->getUrl() ?>">
        <?php echo $this->htmlEscape($category->getName()) ?>
    </a>
<?php endforeach; ?>
14
  • So, how it could be the php code, when I like to list all subcategories on a cms page? Aug 19, 2015 at 8:42
  • You can't write PHP code in a CMS page. But you can create a phtml template and include it in the CMS page with the {{block}} directive. Use the PHP code from my answer in that template, the only thing missing is initialization of $parentCategory (for example: $parentCategory = Mage::getModel('catalog/category')->load(3);) Aug 19, 2015 at 8:45
  • If I put this, I just get nothing, what else do I need to set? I use it on a cms page: <?php $subcategoryCollection = Mage::getModel('catalog/category') ->getCollection() ->addFieldToFilter('level', $parentCategory->getLevel() + 2) ->addFieldToFilter('path', ['like' => $parentCategory->getData('path') . '%']); <?php foreach ($subcategoryCollection as $category): ?> <a href="<?php echo $category->getUrl() ?>"> <?php echo $this->htmlEscape($category->getName()) ?> </a> <?php endforeach; ?> Aug 19, 2015 at 8:54
  • You can't write PHP code in a CMS page Aug 19, 2015 at 8:57
  • yes, I do like this. so, it will be like: <?php $parentCategory = Mage::getModel('catalog/category')->load(3); $subcategoryCollection = Mage::getModel('catalog/category') ->getCollection() ->addFieldToFilter('level', $parentCategory->getLevel() + 1) // remove this to go deeper ->addFieldToFilter('path', ['like' => $parentCategory->getData('path') . '%']); foreach ($subcategoryCollection as $category): ?> <a href="<?php echo $category->getUrl() ?>"> <?php echo $this->htmlEscape($category->getName()) ?> </a> <?php endforeach; ?> Aug 19, 2015 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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