If you are having a little trouble getting the top navigation to ignore the sort order and use alphabetical sort, here is one option you can use…
Consider the following code for a navigation menu…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<ul id="custom-menu"> <?php $_helper = Mage::helper('catalog/category') ?> <?php $_categories = $_helper->getStoreCategories(true, true, false) ?> <?php $currentCategory = Mage::registry('current_category') ?> <?php if (count($_categories) > 0): ?> <ul style="width:940px;"> <?php foreach($_categories as $_category): ?> <li> <b><a class="drop" href="<?php echo $_helper->getCategoryUrl($_category) ?>"> <?php echo $_category->getName() ?> </a></b> <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?> <?php $collection = $_category->getCollection(); $_subcategories = $_category->getChildrenCategories() ?> <?php if (count($_subcategories) > 0): ?> <div class="dropdown_1column" style="width:235px;"> <?php foreach($_subcategories as $_subcategory): ?> <div class="col_1"> <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"> <?php echo $_subcategory->getName() ?> </a> </div> <?php endforeach; ?> <div class="clr" clear="all"></div> </div> <?php endif; ?> </li> <?php endforeach; ?> </ul> <?php endif; ?> |
The problem is when you get down to the secondary level where it you use the getChildrenCategories(). This function only seems to respect sort order and doesn’t seem to have an option to sort by name. So if you look at the first call for the top level categories $_helper->getStoreCategories(true, true, false). The first parameter tells that section to sort by name.
Seeing as this only traverses the top level, the remaining subcategories are left unchanged. After searching all of the forums for a way around it, some people suggested changing the core file /Mage/Catalog/Model/Resource/Category.php and directly editing the getChildrenCategories() function. While this works… the best option would be to copy the contents of that function directly into the template, to keep it upgrade safe.
So this what that would like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<ul id="custom-menu"> <?php $_helper = Mage::helper('catalog/category') ?> <?php $_categories = $_helper->getStoreCategories(true, true, false) ?> <?php $currentCategory = Mage::registry('current_category') ?> <?php if (count($_categories) > 0): ?> <ul style="width:940px;"> <?php foreach($_categories as $_category): ?> <li> <b><a class="drop" href="<?php echo $_helper->getCategoryUrl($_category) ?>"> <?php echo $_category->getName() ?> </a></b> <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?> <?php $collection = $_category->getCollection(); $_subcategories = $collection ->addAttributeToSelect('name') ->addAttributeToSelect('all_children') ->addAttributeToSelect('is_anchor') ->addAttributeToFilter('is_active', 1) ->addAttributeToSort('name','ASC') ->addIdFilter($_category->getChildren()) ->setOrder('name', Varien_Db_Select::SQL_ASC) ->joinUrlRewrite() ->load(); //$_subcategories = $_category->getChildrenCategories() ?> <?php if (count($_subcategories) > 0): ?> <div class="dropdown_1column" style="width:235px;"> <?php foreach($_subcategories as $_subcategory): ?> <div class="col_1"> <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"> <?php echo $_subcategory->getName() ?> </a> </div> <?php endforeach; ?> <div class="clr" clear="all"></div> </div> <?php endif; ?> </li> <?php endforeach; ?> </ul> <?php endif; ?> |
So instead of calling $_subcategories = $_category->getChildrenCategories()… you just set the $_subcategories variable to the following:
1 2 3 4 5 6 7 8 9 10 |
$_subcategories = $collection ->addAttributeToSelect('name') ->addAttributeToSelect('all_children') ->addAttributeToSelect('is_anchor') ->addAttributeToFilter('is_active', 1) ->addAttributeToSort('name','ASC') ->addIdFilter($_category->getChildren()) ->setOrder('name', Varien_Db_Select::SQL_ASC) ->joinUrlRewrite() ->load(); |