Kevin S
Kevin S

Reputation: 95

Categories in alphabetical order (magento)

I am trying to list all the categories in alphabetical order in magento but it is not working.

<?php foreach ($this->getStoreCategories() as $_category): ?>
            <?php
                //arrange by Letter
                foreach($_category->getChildren() as $_sub){
                    $letter = substr(strtolower($_sub->getName()),0,1);
                    $subCategories[$letter][0]['name'] = $letter; //0 index is the letter
                    $i = 1;
                    while(isset($subCategories[$letter][$i])){
                        $i++;
                    }
                    $subCategories[$letter][$i]['name'] = $_sub->getName();
                    $subCategories[$letter][$i]['url'] = $this->getCategoryUrl($_sub);
                }
            ?>

Did I miss anything here?

Upvotes: 0

Views: 2622

Answers (3)

Mahmood Rehman
Mahmood Rehman

Reputation: 4331

Try this code :

    <?php 

$cats = Mage::getModel('catalog/category')->load('2')->getChildren();
$catIds = explode(',',$cats);

$categories = array();
foreach($catIds as $catId) {
       $category = Mage::getModel('catalog/category')->load($catId); 
       $categories[$category->getName()] = $category->getUrl();
}

ksort($categories, SORT_STRING);
?>

<ul>
<?php foreach($categories as $name => $url): ?>
    <li>
    <a href="<?php echo $url; ?>"><?php echo $name; ?></a>
    </li>
<?php endforeach; ?>
</ul>

Upvotes: 2

Marius
Marius

Reputation: 15216

try this:

$root = Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect('name')
    ->addPathsFilter('1/'.$root)
    ->addFieldToFilter('level', array('gt'=>2))
    ->addIsActiveFilter()
    ->addAttributeToSort('name', 'ASC');

This should get you all the active categories in the current store sorted by name, ignoring the level.

Upvotes: 0

Ram Sharma
Ram Sharma

Reputation: 8819

I feel you should try this one.

$allCategories = Mage::getResourceModel('catalog/category_collection')
              ->addAttributeToSelect('name')
              ->addAttributeToSort('name','ASC');

Upvotes: 0

Related Questions