1
votes

im using magento. This is the code that loads my left navigational bar. It loads the main categories into a ol. I want to also make it load the child categories within each li. Where I have put LOAD CHILD CATEGORIES HERE I would like to know the php code to fetch and display all the child categories of the above parent category. I have found several fixes but not knowing pHp very well has resulted in a series of errors.

/**
 * Category left navigation
 *
 * @see Mage_Catalog_Block_Navigation
 */


if (!Mage::registry('current_category')) {
    //no current category selected so default to showing the Products category
    $cur_category=Mage::getModel('catalog/category')->load(51);
    $layer = Mage::getSingleton('catalog/layer');
    $layer->setCurrentCategory($cur_category);
}else{
    //current category selected so display the child categories for it
    $layer = Mage::getSingleton('catalog/layer');
    $_category = $layer->getCurrentCategory();
    $currentCategoryId= $_category->getId();

    if($currentCategoryId == 306){
        //best sellers is selected so show the Product category
        $cur_category=Mage::getModel('catalog/category')->load(51);
        $layer = Mage::getSingleton('catalog/layer');
        $layer->setCurrentCategory($cur_category);
    }
}

$_categories=$this->getCurrentChildCategories();
$_count = is_array($_categories)?count($_categories):$_categories->count();
if($_count):
?>
<div class="box layered-nav">
    <div class="head">
        <h3><?php echo $this->__('Browse By') ?> PRODUCT CATEGORY</h3>
    </div>
    <div class="border-creator">
        <div class="narrow-by">
            <dl id="narrow-by-list">
                <dd>
                    <ol>
                    <?php foreach ($_categories as $_category): ?>
                        <?php if($_category->getIsActive()): ?>
                        <li>                        
                            <a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_category->getName()) ?></a>
                        **LOAD CHILD CATEGORIES IN HERE**
                        </li>
                        <?php endif; ?>
                    <?php endforeach ?>
                    </ol>
                </dd>
            </dl><script type="text/javascript">decorateDataList('narrow-by-list')</script>
        </div>
    </div>
</div>
<?php
endif; 
?>
<!-- [ends] .browse-by // -->

So far the best I come up with, I know its not much

<ul class="subnav">
<li><a></a>
</ul>

I appreciate any help immensely

2

2 Answers

0
votes

Check the following code

 <?php $_helper = Mage::helper('catalog/category'); 
 $_categories = $_helper->getStoreCategories(); 
 $currentCategory = Mage::registry('current_category') ;
 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): ?>
                    <ul>
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <li>
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                    <?php echo $_subcategory->getName() ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Now $_subcategories = $_category->getChildrenCategories() fetches all subcategories.

0
votes

Thanks Chanz i used your code snippet and eventually came up with the following. Took a while cus im a little slow but you gave me what i needed. Much apreciated.

/**
 * Category left navigation
 *
 * @see Mage_Catalog_Block_Navigation
 */


if (!Mage::registry('current_category')) {
    //no current category selected so default to showing the Products category
    $cur_category=Mage::getModel('catalog/category')->load(51);
    $layer = Mage::getSingleton('catalog/layer');
    $layer->setCurrentCategory($cur_category);
}else{
    //current category selected so display the child categories for it
    $layer = Mage::getSingleton('catalog/layer');
    $_category = $layer->getCurrentCategory();
    $currentCategoryId= $_category->getId();

    if($currentCategoryId == 306){
        //best sellers is selected so show the Product category
        $cur_category=Mage::getModel('catalog/category')->load(51);
        $layer = Mage::getSingleton('catalog/layer');
        $layer->setCurrentCategory($cur_category);
    }
}

$_categories=$this->getCurrentChildCategories();
$_count = is_array($_categories)?count($_categories):$_categories->count();
if($_count):
?>
<div class="box layered-nav">
    <div class="head">
        <h3><?php echo $this->__('') ?> PRODUCT CATEGORIES</h3>
    </div>
    <div class="border-creator">
        <div class="narrow-by">
            <dl id="narrow-by-list">
                <dd>
                    <ol>
                    <?php foreach ($_categories as $_category): ?>
                        <?php if($_category->getIsActive()): ?>
                        <li>                        
                            <a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_category->getName()) ?></a>
                            <?php $_subcategories = $_category->getChildrenCategories() ?>
                            <?php if (count($_subcategories) > 0): ?>
                                <ul>
                                    <?php foreach($_subcategories as $_subcategory): ?>
                                    <?php if($_category->getIsActive()): ?>
                                    <li>
                                        <a href="<?php echo $this->getCategoryUrl($_subcategory) ?>"<?php if ($this->isCategoryActive($_subcategory)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_subcategory->getName()) ?></a>
                                        </a>
                                    </li>
                                    <?php endif; ?>
                                    <?php endforeach; ?>
                                </ul>
                            <?php endif; ?>
                        </li>
                        <?php endif; ?>
                    <?php endforeach; ?>
                    </ol>
                </dd>
            </dl><script type="text/javascript">decorateDataList('narrow-by-list')</script>
        </div>
    </div>
</div>
<?php
endif; 
?>
<!-- [ends] .browse-by // -->