Matthew Dolman
Matthew Dolman

Reputation: 1800

Getting Category Active Status from catalog/category model

I am trying to build an array of the active categories on level2.

The problem I have is that the model is returning categories that are no longer active and I do not see a way to filter them.

    $storeId   = Mage::app()->getStore()->getId();

    $category = Mage::getModel('catalog/category')->setStoreId($storeId);
    $categoryCollection = $category->getCollection();
    $categoryCollectionIds = $categoryCollection->getAllIds();

    $level2Categories = array();

    foreach($categoryCollectionIds as $categoryId){
        $category->load($categoryId);
        if($category->getLevel() == '2'){
            $level1Categories[$categoryId] = $category->getName(); 

        }
    }

    echo "<pre>";
    print_r($level1Categories);
    echo "</pre>";

Any ideas on how to achieve this?

Upvotes: 1

Views: 2233

Answers (1)

Rohit S
Rohit S

Reputation: 1040

You can filter your category collection to display active categories using the method addIsActiveFilter()

$categoryCollection = $category->getCollection()->addIsActiveFilter();

Upvotes: 2

Related Questions