Ryan
Ryan

Reputation: 3926

Getting all Magento categories with their names

I'm trying to get all the categories in my Magento website. I've achieved that but they only come back with limited data. I need the name of the category which I'm missing. I've tried using addAttributeToSelect but that doesn't make any difference. Here is my code.

$cats = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->getData();
print_r($cats);

Upvotes: 0

Views: 372

Answers (2)

Keyur Shah
Keyur Shah

Reputation: 11533

http://magentotutorialbeginners.blogspot.in/2014/03/magento-all-categories-with-their-names.html

$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('id')
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url')
->addAttributeToSelect('is_active');

foreach ($categories as $category)
{
    if ($category->getIsActive()) { 
        $name = $category->getName();
    }
}

Upvotes: 1

Marius
Marius

Reputation: 15206

Your code is OK. Except the print_r.
You are printing a collection object and you need to print the items in it.

$cats = Mage::getModel('catalog/category')
         ->getCollection()
         ->addAttributeToSelect('*');//or use 'name' instead of '*'

foreach ($cats as $cat){
    echo 'ID: '.$cat->getId().' Name: '.$cat->getName().'<br />';
}

Upvotes: 2

Related Questions