Created
July 10, 2012 06:45
-
-
Save jcobb/3081631 to your computer and use it in GitHub Desktop.
Custom Magento Category Navigation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="category-navigation"> | |
<?php | |
// get the root category id | |
$root_cat_id = Mage::app()->getStore()->getRootCategoryId(); | |
// get the current category object and ID | |
$current_cat_obj = Mage::registry('current_category'); | |
$current_cat_id = $current_cat_obj->getId(); | |
// get the ID of the parent category from the current category | |
$parent_cat_id = $current_cat_obj->getParentCategory()->getId(); | |
$parent_cat_obj = Mage::getModel('catalog/category')->load($parent_cat_id); | |
// get the sub categories of the current category | |
$sub_cat_obj = $current_cat_obj->getChildrenCategories(); | |
if($current_cat_obj->hasChildren()){ | |
// if the current category obj has children, use that | |
$menu_cats = $current_cat_obj->getChildrenCategories(); | |
$menu_top_level = $current_cat_obj; | |
}else{ | |
// else use the parent object | |
$menu_cats = $parent_cat_obj->getChildrenCategories(); | |
$menu_top_level = $parent_cat_obj; | |
} | |
// top level details | |
$top_level_name = $menu_top_level->getName(); | |
$top_level_url = $menu_top_level->getUrl(); | |
$top_level_id = $menu_top_level->getId(); | |
// is the top level the root? | |
if($top_level_id == $root_cat_id){ | |
echo '<ul><li class="current">'.$current_cat_obj->getName().'</li></ul>'; | |
} | |
else{ | |
// start navigation | |
echo '<ul>'; | |
// check if the top level is the current category and output accordingly | |
if($current_cat_id == $top_level_id){ | |
echo '<li class="current">View All</li>'; | |
} | |
else{ | |
echo '<li><a href="'.$top_level_url.'">View All</a></li>'; | |
} | |
// loop over all the sub category objects | |
foreach($menu_cats as $key => $sub_cat){ | |
$cat_link = $sub_cat->getUrl(); | |
$cat_name = $sub_cat->getName(); | |
$cat_id = $sub_cat->getId(); | |
// check if the sub category is the current category and output accordingly | |
if($current_cat_id == $cat_id){ | |
echo '<li class="current">'.$cat_name.'</li>'; | |
} | |
else{ | |
echo '<li><a href="'.$cat_link.'">'.$cat_name.'</a></li>'; | |
} | |
} | |
// end navigation | |
echo '</ul>'; | |
} | |
?> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use the XML snippet above in the left reference of catalog.xml to make the custom template appear in the left sidebar of all category and product pages.