Friday 26 September 2014

Magento Category Accordian with Alphabatically sorting.

Add following code in any PHTML file and call in left/right side.

<div class="block block-left-nav" id="sidebox">
    <div class="block-title">
        <strong><span><?php echo $this->__('categories') ?></span></strong>
    </div>
    <div class="block-content">
        <ul id="nav-left">
            <?php
            $collection = Mage::getModel('catalog/category')->getCollection()
                    ->addAttributeToSelect('*')
                    ->addAttributeToSelect('is_active')
                    ->addAttributeToSort('name', 'ASC');
            foreach ($collection as $_category1):
                if ($this->getRequest()->getModuleName() == 'vendor') {
                    $_category = Mage::getModel('catalog/category')->load($_category1['entity_id']);
                    ?>
                    <?php if ($_category->getIsActive() && $_category1['entity_id'] != 2 && $_category->getLevel() == 2) { ?>
                        <?php $potential = $_category->hasChildren(); ?>
                        <li><a id="block-title" class="<?php echo ($open) ? 'active' : 'inactive' ?>" href="<?php
                            if ($potential) {
                                echo $this->getCategoryUrl($_category);
                                //echo 'javascript:void(0)';
                            } else {
                                echo $this->getCategoryUrl($_category);
                            }
                            ?> "><?php echo $_category->getName(); ?></a>
                               <?php if ($potential) { ?>
                                <ul class="top-level">
                                    <?php
                                    $_cat_level_2 = array();
                                    foreach ($_category->getChildrenCategories() as $_category2) {
                                        $_cat_level_2[$_category2->getName()] = $_category2;
                                    }
                                    ksort($_cat_level_2);
                                    ?>
                                    <?php foreach ($_cat_level_2 as $subcategory): ?>
                                        <li class="second-category">
                                            <a  class="<?php echo ($open) ? 'active' : '' ?>" href="<?php echo $this->getCategoryUrl($subcategory); ?>"><?php echo $subcategory->getName(); ?></a>
                                            <?php if ($potential) { ?>
                                                <ul class="sub-level">
                                                    <?php
                                                    $_cat_level_3 = array();
                                                    foreach ($subcategory->getChildrenCategories() as $_category3) {
                                                        $_cat_level_3[$_category3->getName()] = $_category3;
                                                    }
                                                    ksort($_cat_level_3);
                                                    ?>
                                                    <?php foreach ($_cat_level_3 as $subsubcategory): ?>
                                                        <li><a class="<?php echo ($open) ? 'active' : '' ?>" href="<?php echo $this->getCategoryUrl($subsubcategory); ?>"><?php echo $subsubcategory->getName(); ?></a></li>
                                                    <?php endforeach; ?>
                                                </ul>
                                            <?php } ?>  
                                        </li>
                                    <?php endforeach; ?>
                                </ul>
                            <?php } ?>
                        </li>
                        <?php
                    }
                }
                else {
                    $_category = Mage::getModel('catalog/category')->load($_category1['entity_id']);
                    ?>
                    <?php if ($_category->getIsActive() && $_category1['entity_id'] != 2 && $_category->getLevel() == 2) { ?>
                        <?php
                        $open = $this->isCategoryActive($_category);
                        if (Mage::registry('current_category')->getid() == $_category1['entity_id']) {
                            $open = 'yes';
                        }
                        ?>
                        <?php $potential = $_category->hasChildren(); ?>
                        <li><a id="block-title" class="<?php echo ($open) ? 'active' : 'inactive' ?>" href="<?php
                            if ($open) {
                                echo $this->getCategoryUrl($_category);
                            } elseif ($potential) {
                                //echo 'javascript:void(0)';
                                echo $this->getCategoryUrl($_category);
                            } else {
                                echo $this->getCategoryUrl($_category);
                            }
                            ?> "><?php echo $_category->getName(); ?></a>
                               <?php if ($potential) { ?>
                                   <?php
                                   $_cat_level_2 = array();
                                   foreach ($_category->getChildrenCategories() as $_category2) {
                                       $_cat_level_2[$_category2->getName()] = $_category2;
                                   }
                                   ksort($_cat_level_2);
                                   ?>
                                <ul class="top-level">
                                    <?php foreach ($_cat_level_2 as $subcategory): ?>
                                        <?php
                                        $open = $this->isCategoryActive($subcategory);
                                        if (Mage::registry('current_category')->getid() == $subcategory->getId()) {
                                            $open = 'yes';
                                        }
                                        ?>
                    <?php $potential = $subcategory->hasChildren(); ?>
                                        <li class="second-category">
                                            <a  class="<?php echo ($open) ? 'active' : '' ?>" href="<?php echo $this->getCategoryUrl($subcategory); ?>"><?php echo $subcategory->getName(); ?></a>
                                                <?php if ($potential) { ?>
                                                <ul class="sub-level">
                                                    <?php
                                                    $_cat_level_3 = array();
                                                    foreach ($subcategory->getChildrenCategories() as $_category3) {
                                                        $_cat_level_3[$_category3->getName()] = $_category3;
                                                    }
                                                    ksort($_cat_level_3);
                                                    ?>
                                                    <?php foreach ($_cat_level_3 as $subsubcategory): ?>
                                                        <?php
                                                        $open = $this->isCategoryActive($subsubcategory);
                                                        if (Mage::registry('current_category')->getid() == $subsubcategory->getId()) {
                                                            $open = 'yes';
                                                        }
                                                        ?>
                                                        <li><a class="<?php echo ($open) ? 'active' : '' ?>" href="<?php echo $this->getCategoryUrl($subsubcategory); ?>"><?php echo $subsubcategory->getName(); ?></a></li>
                                                <?php endforeach; ?>
                                                </ul>
                                        <?php } ?>  
                                        </li>
                                <?php endforeach; ?>
                                </ul>
                        <?php } ?>
                        </li>
                    <?php }
                }
                ?>
<?php endforeach; ?>
        </ul>
    </div>
</div>