Thursday 16 February 2012

Sort a category and subcategory list array alphabetically in Magento

If you are dealing with magento category i am very sure that you’ll face this problem .Magento category fetching is not an issue its a child play but sorting thse categories is little tricky. see below code for category sort in alphabetic order.
<?php
$cats = Mage::getModel('catalog/category')->load(2)->getChildren();
$catIds = explode(',', $cats);

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

    $subcats = Mage::getModel('catalog/category')->load($catId)->getChildren();
    $subcatIds = explode(',', $subcats);
    if ($subcats) {
        foreach ($subcatIds as $subcatId) {
            $subCategory = Mage::getModel('catalog/category')->load($subcatId);
            $subCategories[$category->getName()][$subCategory->getName()] = $subCategory->getUrl();
        }
        ksort($subCategories[$category->getName()], SORT_STRING);
    }
}

ksort($categories, SORT_STRING);
?>                                
    <?php foreach ($categories as $name => $url): ?>
  • <?php echo $name; ?> <?php foreach ($subCategories as $key => $values) { if($name == $key) { echo '"; } } ?>
  • <?php endforeach; ?>

14 comments:

  1. Nice! Is there a way to show sub/child categories too?

    ReplyDelete
    Replies
    1. Check above code i have changed with subcategory list. If you like this than Please follow blog.

      Delete
    2. Hi,
      were do I put this code?
      Thanks

      Delete
    3. Hi,

      Put this code into phtml file where you have to display category in alphabetical order. In my case i want to display category in left side so i put above code into left.phtml file.

      Delete
  2. Hi how can i apply filter on any category!

    ReplyDelete
  3. Can you please post about filters?

    ReplyDelete
  4. Thanks ! If we want to add the "product count" of the subcategory, how can we do this ?

    ReplyDelete
  5. @Raphael: you can use $category->getProductCount(); for products count in category. I hope this will work.

    ReplyDelete
  6. Hi
    i am using magebuzz extension and for getting all active category i customize it but my requirement category name in asc order but i am unable to solve it.

    can you help me ?

    ReplyDelete
    Replies
    1. Hi yogesh,

      I think this code will work you have to change position by name.
      $category = Mage::getModel('catalog/category')->load(3);
      $children = Mage::getModel('catalog/category')->getCollection()->setStoreId(Mage::app()->getStore()->getId());
      $children->addAttributeToSelect('*')
      ->addAttributeToFilter('parent_id', $category->getId())
      ->addAttributeToFilter('is_active', 1)//get only active categories if you want
      ->addAttributeToSort('position');//sort by position

      foreach ($children as $child){
      //do something with $child
      }

      Delete
    2. This comment has been removed by the author.

      Delete
  7. In which file i have to change this send me the folder settings

    ReplyDelete
    Replies
    1. Hi,

      Put this code into phtml file where you have to display category in alphabetical order. In my case i want to display category in left side so i put above code into left.phtml file.

      Delete