5/17/2011

How to get category or subcategory postioned by the admin panel in magento

When we load a category it shows his subcategory according to the product id. If you wish to show category according to the order which you have in you admin panel then you have to fetch your category with little bit different style.See the below code in which I have fetched all subcategory with proper order

<?php
$obj= new Mage_Catalog_Block_Navigation();
$category = $obj->getStoreCategories();
foreach($category as $_category)
{
$catid = $_category->getId();
$parentCategory = Mage::getModel('catalog/category')->load($catid);
$childrenIdArray = explode(',',$parentCategory->getChildren());
if ($key = array_search($parentCategory->getId(),$childrenIdArray)) {
unset($childrenIdArray[$key]);
}
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('entity_id', array('in' => $childrenIdArray))
->addAttributeToSelect('*');

$sortedcategory = $collection->addOrderField('position')->getData();
foreach($sortedcategory as $sorted)
{
$_subcat = Mage::getModel('catalog/category')->load($sorted['entity_id']);
$subcatname = $_subcat->getName();
echo "<h3>".$subcatname."</h3>";
}
}
?>

No comments:

Post a Comment