How to show Top Menu Alphabetically in Magento?
This is very simple just copy below code and paste in topmenu.phtml. Here will show up to third level. If don’t need third level just remove sub sub category code.
<?php # get category list ?>
<?php $_helper = Mage::helper(‘catalog/category’) ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?phpfunction array_sort($array, $on, $order=SORT_ASC)
{
$new_array = array();
$sortable_array = array();if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}return $new_array;
}?>
<?php
$layer = Mage::getSingleton(‘catalog/layer’);
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
?>
<div>
<ul id=”nav”>
<li><a href=”<?php echo $this->getUrl(”)?>” title=”Home”><img src=”/ambler/skin/frontend/ambler/ambler_surgical/images/home-icon.png” alt=”home icon” /></a></li>
<?php $_helper = Mage::helper(‘catalog/category’) ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry(‘current_category’) ?>
<?php if (count($_categories) > 0): ?>
<?php foreach($_categories as $_category): ?>
<?php $_category = Mage::getModel(‘catalog/category’)->load($_category->getId()) ?>
<li><a href=”<?php echo $_helper->getCategoryUrl($_category) ?>”><span><?php echo $_category->getName(); ?></span></a>
<?php $catList = array();?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php foreach($_subcategories as $_subCategory) : ?>
<?php $catList[] = array(‘name’ => $_subCategory->getName(), ‘url’ => $_subCategory->getUrl(), ‘id’ => $_subCategory->getId());?>
<?php endforeach ?>
<?php $catList = array_sort($catList, ‘name’, SORT_ASC);?>
<ul>
<?php if (count($catList) > 0): ?>
<?php $subcat=0?>
<?php foreach($catList as $_subCategory): ?>
<li><a href=”<?php echo $_subCategory[‘url’] ?>”><?php echo $_subCategory[‘name’] ?></a>
<?php $subCatList = array();?>
<?php $_subSubCat = Mage::getModel(‘catalog/category’)->load($_subCategory[‘id’]);
$_subSubCategories = $_subSubCat->getChildrenCategories();?>
<?php foreach($_subSubCategories as $_subSubCategory) : ?><?php $subCatList[] = array(‘name’ => $_subSubCategory[‘name’], ‘url’ => $_subSubCategory[‘url’]);?>
<?php endforeach ?>
<?php $subCatList = array_sort($subCatList, ‘name’, SORT_ASC);?>
<?php if (count($subCatList) > 0): ?>
<ul>
<?php foreach($subCatList as $_subSubCat): ?>
<li><a href=”<?php echo $_subSubCat[‘url’] ?>”><?php echo $_subSubCat[‘name’] ?></a>
<?php endforeach; ?>
</li></ul>
<?php endif; ?>
</li>
<?php endforeach; ?><?php endif; ?>
</ul>
</li>
<?php endforeach; ?><?php endif; ?>
</ul>
</div>
Hope it will work.