rishu
rishu

Reputation: 17

Magento only parent category id

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)

  1. Furniture (only this id) a. living room (not this subcategories) b. Bedroom (not this one)

I need Only top level categories id not the children Please help me doing so Thanks

Upvotes: 0

Views: 1927

Answers (3)

Haijerome
Haijerome

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

Mufaddal
Mufaddal

Reputation: 5381

use below code

$_categories=Mage::getModel('catalog/category')
                ->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('level',2)
                ->addIsActiveFilter();

Upvotes: 0

Rohit S
Rohit S

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

Related Questions