How to order categories in top navigation or sidebar

Last modified by amartinez on Thu, June 24, 2010 17:08
Source|Old Revisions  

This is an old revision of the document!


Edit your template/catalog/navigation/top.phtml and sidebar.phtml

To make it simple, we will sort it like a normal Array:

My app/design/frontend/default/f002/template/catalog/navigation/top.phtml:

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category   design_default
 * @package    Mage
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php 
/**
 * Top menu for store
 *
 * @see Mage_Catalog_Block_Navigation
 */
?>
<div class="header-nav-container">
    <div class="header-nav">
        <h4 class="no-display"><?php echo $this->__('Category Navigation:') ?></h4>
        <ul id="nav">
        <li><a href="<?php echo $this->getUrl() ?>"><?php echo $this->__('Home') ?></a></li>
        <li onmouseover="toggleMenu(this,1)" onmouseout="toggleMenu(this,0)">
            <a href="<?php echo $this->getUrl('') ?>"><?php echo $this->__('Products') ?></a>
            <ul>
                <?php
                function ordena_categorias(&$array, $subkey="id", $sort_ascending=true) {
                    if (count($array))
                        $temp_array[key($array)] = array_shift($array);
                    foreach($array as $key => $val){
                        $offset = 0;
                        $found = false;
                        foreach($temp_array as $tmp_key => $tmp_val) {
                            if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey])) {
                                $temp_array = array_merge((array)array_slice($temp_array,0,$offset), array($key => $val), array_slice($temp_array,$offset));
                                $found = true;
                            }
                            $offset++;
                        }
                        if(!$found)
                            $temp_array = array_merge($temp_array, array($key => $val));
                    }
                    if ($sort_ascending)
                        $array = array_reverse($temp_array);
                    else
                        $array = $temp_array;
                }
                $temp = Array();
                foreach ($this->getStoreCategories('name') as $_category):
                    $temp[] = $_category;
                endforeach;
                ordena_categorias($temp, "name");
                
                foreach ($temp as $_category):
                    echo $this->drawItem($_category);
                endforeach;
                ?>
            </ul>
        </li>
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('menu')->toHtml(); ?>
        </ul>
    </div>
    <?php echo $this->getChildHtml('topLeftLinks') ?>
</div>

My app/design/frontend/default/f002/template/catalog/navigation/sidebar.phtml:

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category   design_default
 * @package    Mage
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php 
/**
 * Top menu for store
 *
 * @see Mage_Catalog_Block_Navigation
 */
?>

<div class="box base-mini mini-navigation">
    <div class="head"><h4>Store Catalog</h4></div>

    <div class="content">
        <ul id="nav-sidebox">
                <?php
                function ordena_categorias_lateral(&$array, $subkey="id", $sort_ascending=true) {
                    if (count($array))
                        $temp_array[key($array)] = array_shift($array);
                    foreach($array as $key => $val){
                        $offset = 0;
                        $found = false;
                        foreach($temp_array as $tmp_key => $tmp_val) {
                            if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey])) {
                                $temp_array = array_merge((array)array_slice($temp_array,0,$offset), array($key => $val), array_slice($temp_array,$offset));
                                $found = true;
                            }
                            $offset++;
                        }
                        if(!$found)
                            $temp_array = array_merge($temp_array, array($key => $val));
                    }
                    if ($sort_ascending)
                        $array = array_reverse($temp_array);
                    else
                        $array = $temp_array;
                }
                $temp = Array();
                foreach ($this->getStoreCategories('name') as $_category):
                    $temp[] = $_category;
                endforeach;
                ordena_categorias_lateral($temp, "name");
                
                foreach ($temp as $_category):
                    echo $this->drawItem($_category);
                endforeach;
                ?>
        </ul>
    </div>
</div>

I hope it helps

amartinez

amartinez [at] reinizia [dot] com




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs