2

I have currently implemented a code which shows all categories on the left nav instead of that I want current category and current category's all sub categories.

Now my category structure is like

-parent1
 -sub 1
  -sub sub 1
 -sub 2
-parent2
 -sub 3

Now if i am on page Sub1 then I must be able to see all sub categories of sub 1 and also the sub categories of parent1 If I am on sub sub 1 then I must be able to see sub categories of sub1 .

I have currently implemented the following code

<!-- List all categories and their second level subcategories -->
<div class="block block-list block-categories">
    <div id="block-categories" class="block-title active">
        <strong><span>Categories </span></strong>
    </div>

<div id="leftnav" class="block-content" style="display:block">
    <?php $helper = $this->helper('catalog/category') ?>
    <?php $categories = $this->getStoreCategories() ?>
    <?php if (count($categories) > 0): ?>
        <ul id="leftnav-tree" class="level0">
            <?php foreach($categories as $category): ?>
                <li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>">
                   <a href="<?php echo $helper->getCategoryUrl($category) ?>"><span><?php echo $this->escapeHtml($category->getName()) ?></span></a>
                    <?php //if ($this->isCategoryActive($category)): ?>
                        <?php $subcategories = $category->getChildren() ?>
                        <?php if (count($subcategories) > 0): ?>
                            <ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1">
                                <?php foreach($subcategories as $subcategory): ?>
                                    <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
                                        <a href="<?php echo $helper->getCategoryUrl($subcategory) ?>"><?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?></a>
                                         <?php $secondLevelSubcategories = $subcategory->getChildren() ?>
                                         <?php if (count($secondLevelSubcategories ) > 0): ?>
                            <ul id="leftnav-tree-<?php echo $subcategory->getId() ?>" class="level2">
                                <?php foreach($secondLevelSubcategories as $secondLevelSubcategory ): ?>
                                    <li class="level2<?php if ($this->isCategoryActive($secondLevelSubcategory )): ?> active<?php endif; ?>">
                                        <a href="<?php echo $helper->getCategoryUrl($secondLevelSubcategory ) ?>"><?php echo $this->escapeHtml(trim($secondLevelSubcategory ->getName(), '- ')) ?></a>
                                    </li>
                                    <?php endforeach; ?>
                            </ul>
                            <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
                        <?php endif; ?>
                                <?php endforeach; ?>
                            </ul>
                            <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
                        <?php endif; ?>
                    <?php //endif; ?>
                </li>
            <?php endforeach; ?>
        </ul>
        <script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
    <?php endif; ?>
</div>
</div>

I this code gives all categories including sub categories.

1

if you are extend this class class Mage_Catalog_Block_Navigation

then you can check with the function like below

if ($this->isCategoryActive($category)) {
            $classes[] = 'active';
            $activeClass = ' class="cur-act-cat"';
        }

or if not you can add below two function to your block to check if category is active or not

     /**
     * Check whether specified category is active
     *
     * @param Varien_Object $category
     * @return bool
     */
    public function isCategoryActive($category)
    {
        return $this->getCurrentCategory()
            ? in_array($category->getId(), $this->getCurrentCategory()->getPathIds()) : false;
    }

     /**
     * Check whether specified category is active
     *
     * @param Varien_Object $category
     * @return bool
     */
    public function isCategoryActive($category)
    {
        return $this->getCurrentCategory()
            ? in_array($category->getId(), $this->getCurrentCategory()->getPathIds()) : false;
    }

hope this will sure help you

  • I am not getting where to add the code. Can you please point out where to add? – Bhargav Mehta Jan 2 '15 at 13:20
  • in your block or if your block is extend class Mage_Catalog_Block_Navigation then no need for both function – liyakat Jan 2 '15 at 13:22
  • Then what is the work around? I the demo is installed on infigic.com/ds4u There click on any category then there will be a sidebar visible. but it shows all categories and sub categories – Bhargav Mehta Jan 2 '15 at 13:40
  • @Bhargav, you can download and see extensions code as reference to achieve your functionality. magentocommerce.com/magento-connect/… i am not associated with this extension at any more.it is just for your help – liyakat Jan 3 '15 at 4:08
  • Thanks @liyakat I am currently using that extension only to show sub categories – Bhargav Mehta Jan 3 '15 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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