Magento Themes and Magento Extensions

Magento: Displaying Categories and Current Subcategories

Share

I started “skinning” Magento today with a design that I was given. The functionality called for was a left category navigation (on every page but customer pages and cart/checkout pages) that always displayed the main categories – but when you click on a category, Magento should take you to that category’s listing, but also display the current subcategories.

So – since we all know that the Magento documentation is pretty crappy at this time, I had to do a lot of digging through core files, and a lot of time just with trial and error. But, I finally did come up with something that works. I’m willing to bet that there is a better, more proper way to do it, but regardless, this seems to be working perfectly.

What I did was create a new PHTML file, created the proper block call in page.xml, and now I’ve got a great working (and looking!) left-navigation. Here’s the home page (left), and a category page (right):

And the code:

<h3>Browse by Category:</h3>

<ul>
<?php
$obj = new Mage_Catalog_Block_Navigation();
$store_cats	= $obj->getStoreCategories();
$current_cat 	= $obj->getCurrentCategory();

$current_cat	= (is_object($current_cat) ? $current_cat->getName() : '');

foreach ($store_cats as $cat) {
	if ($cat->getName() == $current_cat) {
		echo '<li><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a>\n<ul>\n";
		foreach ($obj->getCurrentChildCategories() as $subcat) {
			echo '<li><a href="'.$this->getCategoryUrl($subcat).'">'.$subcat
->getName()."</a></li>\n";
		}
		echo "</ul>\n</li>\n";
	} else {
		echo '<li><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a></li>\n";
	}
}
?>
</ul>

Leave a Reply

You must be logged in to post a comment.