941-685-8851

Magento sidebar category navigation 1 level reversed order

November 3, 2011 by Razworks Web Designer Sarasota Comments Off

I needed to create a left sidebar category navigation which display only the top level categories. The category display order in this navigation needed to be reverse that of the default category display order. For example, here is a screen capture of the Manage Categories interface in the Magento administration.

Magento Ecommerce web design

Below is a screen capture of the Magento shopping cart website frontend. Sorting the category order in the Manage Categories administration (shown above) will determine how the categories are sorted on the frontend.

Magento Ecommerce shopping cart website design

The code to create this Magento vertical category navigation is:

<ul id=”category-nav”>
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php if($_category->name!=””):  ?>
<ul id=”category-nav”><?php foreach ($this->getStoreCategories() as $_category): ?><?php if($_category->name!=””):  ?><li><?php echo $this->drawItem($_category) ?></li><?php endif?><?php endforeach ?></ul>
<?php endif?>
<?php endforeach ?>
</ul>

But in the case of this particular ecommerce shopping cart web design, I need the top horizontal navigation to retain the sort order defined in Manage Categories, and the left sidebar vertical navigation to display a reverse order of that defined in Manage categories.

Magento Ecommerce shopping cart website development

The code to create this Magento vertical category navigation with a reverse sort order is:

<ul id=”category-nav”>
<?php $helper = Mage::helper(‘catalog/category’);
$categoryCollection = $helper->getStoreCategories(false,true,false);
$categoryCollection->addAttributeToSort(‘level’, ‘asc’)
->addAttributeToSort(‘position’, ‘desc’)
->addAttributeToFIlter(‘level’, array(‘eq’ => ’2′));  //Added to limit to only level 1 categories.; ?>

<?php foreach ($categoryCollection as $_category): ?>
<?php if($_category->name!=”"):  ?>
<li><a href=”<?php echo $this->getCategoryUrl($_category); ?>”title=”<?php echo $this->htmlEscape($_category->getName(‘position’, ‘desc’)); ?>”>
<?php echo $this->htmlEscape($_category->getName()); ?></a></li>
<?php endif?><?php endforeach ?>
</ul>

Share
 

Comments are closed.