Reputation: 3926
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
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
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