Magento:Display current category and sub categories within current category on layered navigation
Today we will see how to “Display current category and sub categories within current category on layered navigation” , In Magento Suppose you have below categories structure
- Category 1
- Category 1a
- Category 1b
- Category 1c
- Category 1c1
- Category 1c2
- Category 2
- Category 2a
- Category 2b
Now when user id on Category 1c2 page, in layered left navigation it will display Shop By category strcutre including Category 1 and Category 2 and everything inside it.
If you want to filter your layered left navigation so that it only display the the structure of from Root parent category(Category 1 in our case) and all sub categories within this parent category. So in our example like if you wan to display left navigation like below(note category 2 is hidden)
- Category 1a
- Category 1b
- Category 1c
- Category 1c1
- Category 1c2
Below is the code you can use to filter,
<?php $currentCategory = Mage::registry("current_category"); ?> <?php $path = $currentCategory->getPath(); $ids = explode('/', $path); ?> <?php $_helper = Mage::helper('catalog/category') ?> <?php $_categories = $_helper->getStoreCategories() ?> <div class="block block-right-nav block-yt-layered-nav"> <div class="block-layered-nav-inner"> <div class="block-title"> <strong> <span><?php echo $this->__('Categories'); ?></span> </strong> </div> <div class="block-content clearfix"> <?php if (count($_categories) > 0): ?> <ul id="yt_sidenav" class="dropdow-nav"> <?php foreach($_categories as $_category): ?> <?php if (in_array($_category->getId(),$ids)): ?> <?php echo $this->drawItem($_category) ?> <?php endif; ?> <?php endforeach ?> </ul> <?php endif; ?> <script type="text/javascript"> decorateList('yt_sidenav') </script> </div> <?php echo $this->getChildHtml('topLeftLinks'); ?> </div> </div>
Basically what we are doing here, that we are getting current category path(getPath()) and spiting it in to array by ‘/’ and before displaying category inside each loop we are comparing the current category index id is within the array, if so display category else don’t display
Hope this helps…Happy Coding!!!!