filippo90
filippo90

Reputation: 367

Get Magento Sub-Categories in Alphabetical Order

I'm using the code below to get all the subcategories of category '9'.
The code works great, but it sort the subcategory by id or creation date, I need to sort the subcategories alphabetically.

<?php
$categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('parent_id', 9);

foreach ($categories as $cat) { ?>
    <?php 
        $entity_id = $cat->getId();
        $name = $cat->getName();
        $url_key = $cat->getUrlKey();
        $url_path = $cat->getUrlPath();
        $skin_url = $cat->getImageUrl();
    ?>
    <div>
        <a href="<?php echo $url_path ?>">
            <?php 
                echo '<img style="width: 100%;" src="'.$skin_url.'" />';
            ?>

            <div class="brand-border-top"></div>
            <div class="brand-border-bottom"></div>
            <div class="brand-overlay">
                <?php echo $name = $cat->getName(); ?>
            </div>
        </a>
    </div>
<?php } ?>

Upvotes: 0

Views: 783

Answers (1)

Gopal Patel
Gopal Patel

Reputation: 238

Use

->addAttributeToSort('name','ASC');

In your case

$categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('parent_id', 9)->addAttributeToSort('name','ASC');

Upvotes: 1

Related Questions