Magento Themes and Magento Extensions

Magento Vertical Category Menu


Magento Vertical Menu using Peter Hamilton’s code

In one of our premium Magento themes we implemented a vertical category menu with the code adapted from this thread. It’s quite easy to implement and to make it even easier for others, we decided to write a step by step tutorial.

  • Add a new block type in “catalog.xml” file
  • Create a new phtml file and name it “leftnav.phtml”, place it inside catalog/navigation
  • Edit CSS

Add a new block type in “catalog.xml” file

Here we presume you want to have the vertical category menu shows up on every page, to do so, we will place the code in the <default> layout like so, and we want it to always stay on the top, so we added “before=”-”:

2.<!-- Mage_Catalog -->
3.<reference name="right">
4.<block type="catalog/navigation"before="-""leftNav" as="leftNav" template="catalog/navigation/leftnav.phtml"/>

You can change the reference name from ‘right’ to left if you prefer to have the vertical menu placed on the left column.

Create a leftnav.phtml file

This code is copied directly from the above mention thread posted by Peter Hamilton on #63 with a bit of modification to fix the validation error.:

01.<div class="sidebox">
02.<h6>Browse Category</h6>
03.<ul id="side-nav">
04.<?php foreach ($this->getStoreCategories() as $_category): ?>
05.<?php if ($_category->getIsActive()) { ?>
06.<?php $open = $this->isCategoryActive($_category); ?>
07.<?php $potential = $_category->hasChildren(); ?>
08.<li class="subcat"><a href="<?php echo $this->getCategoryUrl($_category); ?>"<?php if($open) { echo ' class="open"'; } ?><?php if($potential) { echo 'class="potential"'; } ?> ><?php echo $_category->getName();?></a>
09.<?php if ($open && $potential): ?>
10.<ul id="sub-sidenav">
11.<?php foreach ($_category->getChildrenCategories() as $subcategory): ?>
12.<?php $subCat = Mage::getModel('catalog/category')->load($subcategory); ?>
13.<?php $open = $this->isCategoryActive($subCat); ?>
14.<?php $potential = $subCat->hasChildren(); ?>
15.<li><a href="<?php echo $this->getCategoryUrl($subCat); ?>"<?php if($open) { echo ' class="subopen"'; } ?><?php if(!$potential&&$open) { echo 'class="final"'; } ?> ><?php if($potential&&$open) { echo ' '; } elseif($potential) { echo ' '; }?><?php echo $subCat->getName(); ?></a>
16.<?php if ($open && $potential): ?>
18.<?php foreach ($subcategory->getChildrenCategories() as $subsubcategory): ?>
19.<?php $subsubCat = Mage::getModel('catalog/category')->load($subsubcategory); ?>
20.<?php $open = $this->isCategoryActive($subsubCat) ?>
21.<li><a href="<?php echo $this->getCategoryUrl($subsubCat); ?>" <?phpif($open) { echo ' class="final"'; } ?>><?php echo $subsubCat->getName(); ?></a></li>
22.<?php endforeach; ?>
24.<?php endif; ?>
26.<?php endforeach; ?>
28.<?php endif; ?>
30.<?php } ?>
31.<?php endforeach ?>

Edit your CSS

01.ul#side-nav li.subcat {background-color:#f5f4f0;border-bottom:1px solid #ddd;
02.color:#222;margin: 0 -5px;display: block;}
03.ul#side-nav li.subcat a {display: block;padding: 2px 10px;font-size: .85em;}
04.ul#side-nav li.subcat:hover {
05.background-color:#fefefe;background-position: 50% top;}
06.ul#side-nav li.subcat ul li {padding-left:18px;
07.line-height: 1.6;background:#fff }
08.ul#side-nav li.subcat ul li a {color: #1a443c;font-weight: bold;
09.font-size: .85em!important;
10.background: url(../images/base_images/icon-arrow-set.png) no-repeat 0 -59px;}
11.ul#side-nav li.subcat ul li:hover {text-decoration: underline;}
12.ul#side-nav li.subcat a ,
13.ul#side-nav li.subcat ul li a:hover {text-decoration: none;}

That’s it! Simple isn’t it?

Known issue in Magento version 1.3

The recent release of Magento v1.3 has introduced Flat Catalog Category and Flat Catalog Product features, and some codes had changed in code/core/Mage/Catalog/Naviation.php that resulting Peter’s code not compatible with v1.3, as such, you need to update:

<?php foreach ($_category->getChildren() as $subcategory): ?>

to <?php foreach ($_category->getChildrenCategories() as $subcategory): ?>

if you wish to switch on Flat Catalog Category; however, we found a bug related to custom options when add to cart is performed with Flat Catalog Category switched on. The bug has been assigned, and before Varien releases an update, your choice is to use the old code with “getChildren” with no Flat Catalog Categoryenables if you have Configurable products or Simple Products with Custom Options setup.

The above leftnav code is for Flat Catalog Category switch on. Simply revert “getChildrenCategories()” to “getChildren()” if you do not want to use Flat Catalog Category feature.

Closing note

The code calls “getStoreCategories” and “getChildrenCategories” and it works a bit similar to the Layered Navigation’s category. Nice it is but has a tiny issue that we didn’t aware, that is, on our Magento New Red Theme, on the homepage, you see Bestsellers and New Products are listed, if you click on any of the products there, in the product page, you do not see the vertical category menu. This is because we didn’t setup categories for “bestsellers” and “new products”, so as far as that product page concerned, it belongs to no category (in another word, none of the categories are ‘active’ – see the code: <?php if ($_category->getIsActive()) { ?> ) therefor vertical menu is not presented.

In conclusion, we think this vertical category menu works best in conjunction with horizontal “topMenu” and should not be a substitute of topMenu.

Leave a Reply

You must be logged in to post a comment.