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; ?>
Nice! Is there a way to show sub/child categories too?
ReplyDeleteCheck above code i have changed with subcategory list. If you like this than Please follow blog.
DeleteHi,
Deletewere do I put this code?
Thanks
Hi,
DeletePut 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.
Hi how can i apply filter on any category!
ReplyDeleteCan you please post about filters?
ReplyDeleteThanks !!!
ReplyDeleteThanks ! If we want to add the "product count" of the subcategory, how can we do this ?
ReplyDelete@Raphael: you can use $category->getProductCount(); for products count in category. I hope this will work.
ReplyDeleteHi
ReplyDeletei 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 ?
Hi yogesh,
DeleteI 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
}
This comment has been removed by the author.
DeleteIn which file i have to change this send me the folder settings
ReplyDeleteHi,
DeletePut 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.