Reputation: 17
Hello i need only top level categories id that is i need only parent category id not the subcategories. such as :
Root Category (No Need of its ID)
I need Only top level categories id not the children Please help me doing so Thanks
Upvotes: 0
Views: 1927
Reputation: 1550
IF am not wrong you are looking only for LEVEL 2 category Ids. If so the code below helps you
$categoryCollection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('level','2')
->addAttributeToSort('name', 'ASC')
->addIsActiveFilter();
foreach($categoryCollection as $cat){
echo '<br/>'.$cat->getId().' | '.$cat->getName();
}
To Get both Level 2 and Leve 4 catgories just use 'in' operator to filer your collection as shown below :
$categoryCollection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('level',array('in'=>('2','4')))
->addAttributeToSort('name', 'ASC')
->addIsActiveFilter();
foreach($categoryCollection as $cat){
echo '<br/>'.$cat->getId().' | '.$cat->getName();
}
Upvotes: 1
Reputation: 5381
use below code
$_categories=Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('level',2)
->addIsActiveFilter();
Upvotes: 0
Reputation: 1040
Use the code below
$children = Mage::getModel('catalog/category')->getCategories(Mage::app()->getStore()->getRootCategoryId());
foreach ($children as $category) {
echo $category->getId();
}
Upvotes: 0