Magento – Product Collection for Subcategories of Subcategories

categorycollection;product-collection

I have the following menu/category structure in my store:

        root
          |
      products
|         |        |
cars    bikes     vans
|         |        |
bmw    carrera    fiat
audi   apollo     ford
ford   raleigh   
lexus  voodoo   
tvr     

I need to be able to access just the subcategories of products subcategories as a collection.

So my page would access that collection and output a simple list like this:

bmw   
audi   
ford   
lexus  
tvr 
carrera
apollo
raleigh
voodoo
fiat
ford

I can get products subcategories in a collection like below but how do I get the subcategories of them?

$category = Mage::getModel('catalog/category')->load($the_id);
$categories = $category->getCollection()
    ->addAttributeToSelect(array('name', 'thumbnail'))
    ->addAttributeToFilter('is_active', 1)
    ->addIdFilter($category->getChildren())

<ul>
<?php
foreach ($categories as $category): ?>
    <li><?php echo $category->getName() ?></li>
<?php endforeach;  ?>
</ul>  

Best Answer

From what I understood you need all the categories that have a certain level, independently of their parent category.

If I got it right, try this:

$categories = Mage::getModel('catalog/category')->getCollection();
$categories->addAttributeToFilter('level', 4);
$categories->addAttributeToFilter('is_active', 1);

This is how you know what level value you need to use.
0 - Root of all roots (not visible in the backend)
1 - Catalog Root
2 - top level categories
3+ - go lower in the category tree.

If you need only the X level categories from a certain root category add these lines to the ones above:

Let's assume that your 'Products' category id is $mainId.

$rootId = Mage::app()->getStore()->getRootCategoryId();
$path = '1/'.$rootId.'/'.$mainId.'/'; //the slash at the end is very important.
$categories->addPathsFilter($path);