0
votes

Looking to dynamically show all subcategories of a current category "brand page" for example, but also, to show each of those subcat's categories, (so two levels shown on the primary top level page.) - like the attached diagram:

on a main category page like with div's or li's:

SUBCATEGORY

Sub-Sub 1, Sub-sub2, Sub-Sub 3

SUBCATEGORY 2

Sub-Sub 1, Sub-sub2, Sub-Sub 3

SUBCATEGORY 3

Sub-Sub 1, Sub-sub2, Sub-Sub 3

I've done a lot of searching and found similar but not exactly the right thing. This was closest thing I could find:

http://magentoo.blogspot.com/2014/01/get-all-subcategories-of-parent-category-magento.html

2

2 Answers

2
votes

I have modified pieces of code from around the web together to make it work without having ti hard-code it into each top level category...

this did the trick:

<?php 

$parentCategoryId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$cat = Mage::getModel('catalog/category')->load($parentCategoryId);
$subcats = $cat->getChildren();

?>

<!--// Get 1 Level sub category of Parent category.-->
<?php 
foreach(explode(',',$subcats) as $subCatid)
              {
                $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive()) {
    //echo '<ul><a href="'.$_category->getURL().'" title="View the products for the "'.$_category->getName().'" category">'.$_category->getName().'</a>';
  // echo '</ul>';
  }
}

?>

<!--// Get 2 Level sub category of Parent sub category-->
<?php 
foreach(explode(',',$subcats) as $subCatid)
{
  $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive()) {
    echo '<ul><li class="subcat-title">'.$_category->getName().'</li>';
    $sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
    $sub_subcats = $sub_cat->getChildren();
    foreach(explode(',',$sub_subcats) as $sub_subCatid)
    {
          $_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
          if($_sub_category->getIsActive()) {
              echo '<li class="sub_cat"><a href="'.$_sub_category->getURL().'" category">'.$_sub_category->getName().'</a></li>';

 }
     }
     echo '</ul>';
  }
}
?>

then styled with some CSS of course

0
votes

I have done something similar to what you are describing by adding a catalog/navigation block at the top of my category pages:

Insert a new block into the layout somewhere like app/design/frontend/$package/$theme/layout/catalog.xml or app/design/frontend/$package/$theme/layout/local.xml. In theory you could also do this on a single category basis by inserting just the reference portion below into the category tab “Custom Design -> Custom Layout Update”:

<catalog_category_view>
    <reference name="content">
        <block type="catalog/navigation" name="catalog.catnav" template="catalog/navigation/category.phtml" before="-"/>
    </reference>
</catalog_category_view>

Then create or modify app/design/frontend/$package/$theme/template/catalog/navigation/category.phtml:

<ul id="catnav"><?= $this->renderCategoriesMenuHtml() ?></ul>

The result should be something like this tree structure on your category page, which you can style with CSS through targeting classes to display as needed:

<ul id="catnav">
    <li class="level0 nav-1 active first last parent">
        <a href="...">ROOT CATEGORY NAME</a>
        <ul class="level0">
            <li class="level1 nav-1-1 first">
                <a href="...">SUBCATEGORY LEVEL 1</a>
            </li>
            <li class="level1 nav-1-2">
                <a href="...">SUBCATEGORY LEVEL 1</a>
            </li>
            <li class="level1 nav-1-3 active parent">
                <a href="...">SUBCATEGORY LEVEL 1 (PARENT + ACTIVE)</a>
                <ul class="level1">
                    <li class="level2 nav-1-3-1 active first">
                        <a href="...">SUBCATEGORY LEVEL 2 (ACTIVE)</a>
                    </li>
                    <li class="level2 nav-1-3-2">
                        <a href="...">SUBCATEGORY LEVEL 2</a>
                    </li>
                </ul>
            </li>
            <li class="level1 nav-1-4 last parent">
                <a href="...">SUBCATEGORY LEVEL 1 (PARENT)</a>
                <ul class="level1">
                    <li class="level2 nav-1-4-1 first">
                        <a href="...">SUBCATEGORY LEVEL 2</a>
                    </li>
                    <li class="level2 nav-1-4-2">
                        <a href="...">SUBCATEGORY LEVEL 2</a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>