0
votes

I've looked around a lot and found lots about pulling in parent Id's and sub cat listings but this is slight different and I don't seem to able to find an answer, I'm not a PHP guru (yet) so go easy on me. I've tried various things but I just end up with errors.

I have created a phtml template to display sub categories of a parent category in an inline link block below my main nav. I'm calling this template with a static block from the admin which is working fine, but the link block disappears when I navigate through to a subcategory page, obviously because this code calls sub cats of a parent but not while you are actually IN a sub cat. here's the code I'm using so far:

<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
    <div class="category-products <?php echo Mage::getModel('catalog/layer')->getCurrentCategory()->getName(); ?>">
        <dl id="narrow-by-list2">
            <dt></dt>
            <dd>
                <ol class="subcat_list">
                <?php foreach ($_categories as $_category): ?>
                    <?php if($_category->getIsActive()): ?>
                    <li>
                        <a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a>
                    </li>
                    <?php endif; ?>
                <?php endforeach ?>
                </ol>
            </dd>
        </dl>
        <script type="text/javascript">decorateDataList('narrow-by-list2')</script>
    </div>
<?php endif; ?>

any ideas as to how I can modify this so the list stays there while I'm actually viewing a sub cat? Many thanks

2

2 Answers

1
votes

After much looking around I finally stumbled on what I needed Thanks MagikSwapna for your input it helped me understand things a little more

I eventually ended up with this

    <div class="category-products <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('parent_cat_name')->toHtml() ?>">
<?php  echo "<ol class='subcat_list'>"; ?>

 <?php
    $currentCat = Mage::registry('current_category');

    if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
    {
        // current category is a toplevel category
        $loadCategory = $currentCat;

    }
    else
    {
        // current category is a sub-(or subsub-, etc...)category of a toplevel category
        // load the parent category of the current category
        $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
        // @TODO enhance for more nested category levels to display sub-categories
    }    
    $subCategories = explode(',', $loadCategory->getChildren());

    foreach ( $subCategories as $subCategoryId )
    {
        $cat = Mage::getModel('catalog/category')->load($subCategoryId);

        if($cat->getIsActive())
        {
            if($crcat == $cat->getName())                                                   //Check if current category is this subcategory
                echo '<li><b><a href="'.$cat->getURL().'">'.$cat->getName().'</a></b>'.'</li>'; //If yes display it as bold (Currently Selected)
            else                                                                            //
                echo '<li><a href="'.$cat->getURL().'">'.$cat->getName().'</a>'.'</li>';        //Otherwise display it as normal
        }
    }

?>
<?php  echo "</ol>"; ?>
</div>

and it's working fine, I created a custom layout to use for the category pages and it will throw an error if used in a non-category related page, but it works! Finally.

0
votes
<?php $_helper = Mage::helper('catalog/category') ?>
    <?php $_categories = $_helper->getStoreCategories() ?>   
    <?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): ?>                                                                                                                                    
                         <?php foreach($_subcategories as $_subcategory): ?>
                                <h3><a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"><?php echo $_subcategory->getName() ?></a></h3>
                                <!--sub sub category-->
                                <?php $_subcategory = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>                             
                                <?php $_subsubcategories = $_subcategory->getChildrenCategories() ?>
                                <?php if (count($_subsubcategories) > 0): ?>
                                   <ul>
                                    <?php foreach($_subsubcategories as $_subsubcategory): ?>
                                       <li>
                                          <a href="<?php echo $_helper->getCategoryUrl($_subsubcategory) ?>"><?php echo $_subsubcategory->getName() ?></a>            
                                          <?php $_subsubsubcategory = Mage::getModel('catalog/category')->load($_subsubcategory->getId()) ?>                             
                                          <?php $_subsubsubcategories = $_subsubcategory->getChildrenCategories() ?>
                                          <?php if (count($_subsubsubcategories) > 0): ?>                                                                                            
                                                   <ul>
                                                     <?php foreach($_subsubsubcategories as $_subsubsubcategory): ?>
                                                       <li>
                                                         <a href="<?php echo $_helper->getCategoryUrl($_subsubsubcategory) ?>"><span><?php echo $_subsubsubcategory->getName() ?></span></a>
                                                       </li> 
                                                     <?php endforeach; ?>
                                                   </ul>                                                                                               
                                          <?php endif; ?>
                                       </li> 
                                    <?php endforeach; ?>
                                   </ul>                                   
                               <?php endif; ?>
                                 <!--sub sub category-->                               
                         <?php endforeach; ?>                                                                                                                                                     
                    <?php endif; ?>
                    </li> 
             <?php endforeach; ?> 
  </ul> 
    <?php endif; ?>