Magento Sub Category Navigation

Friday September 02nd, 2011

A free and simple to use block of PHP to give Magento developer's full control over how sub categories are displayed on their eCommerce website.

Ecommerce

Getting your products online is easier than you may think. We'll guide you through the process and get you up and running quickly.

Ecommerce

For a recent project we were asked to include the functionality to 'drill down' into a category on Magento. When a user entered a category, for example 'Home' they would see a second navigation bar including items such as "Living Room", "Kitchen", "Dining Room". We needed the ability to have complete control over how it was displayed, because although Magento does have similar functionality built in the way it renders was not right for this job.

We wrote a simple block of PHP which renders a simple unordered list of navigation that we then style accordingly. In its current state it's only been tested at one sub category level deep, as the project's scope did not require anything further - however it should be adaptable to your needs.

<?php

/**
 * (C) 2011 Red Carrot
 * Sub Navigation for Categories (beta)
 * ---------------------------------
 *
 * Looks at your current category location and displays a list of all sub categories within this.
 * Originated from: http://fishpig.co.uk/magento-tutorials/display-categories-and-subcategories-in-magento
 * 
 * NOTE: Not designed for sub-sub categories in this version.
 */

$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
$currentCategory = Mage::registry('current_category');

// A quick check to make sure that we are at the top level or not (rather than the theoretical top level in Magento)
$defaultCategory = 2;
$parentCategory = $currentCategory->getParentId();
if($defaultCategory==$parentCategory){
	$topLevel = true;	
} else {
	$topLevel = false;
}

if (count($_categories) > 0){
	foreach($_categories as $_category){
		if (($currentCategory && $currentCategory->getId() == $_category->getId()) || (!$topLevel && $parentCategory == $_category->getId()) ){
			$_category = Mage::getModel('catalog/category')->load($_category->getId());
			$_subcategories = $_category->getChildrenCategories();
			if (count($_subcategories) > 0){
				echo '';
			}
		}
	}
}
?>

0 comments - add your own