Search code examples
magento-1.9

magento show navigation item by product count


In Magento category(Product list) page, categories and attribute options are showing at left sidebar. I want to sort these items by product counts.Please anyone can explain that in which file i have to write code ? Thanks


Solution

  • Try below code once. Change your category ID.

    <?php
        $cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
    ?>
     <ul>   
        <?php
        $resourcearray = array();
        foreach($cats as $category): 
        $proCnt = $category->getProductCount();
        $proName = $category->getName();
        $proUrl = $category->getUrl();
        array_push($resourcearray, array('count' => $proCnt, 'name' => $proName,'url' => $proUrl));
        ?>
        <?php endforeach; 
        ksort($resourcearray);
        ?>
        <?php foreach($resourcearray as $val): ?>
            <li>
                <a href="<?php echo $val['url'] ?>"><?php echo $val['name'] ?>(<?php echo $val['count'] ?>)</a>
            </li>
        <?php endforeach;?>
        </ul>