Magento Forum

Getting the categories of all stores in an array
 
Capitaine Commerce
Member
 
Avatar
Total Posts:  68
Joined:  2007-09-01
Lille, France
 

Simpe question : how do I get all categories from a store in an array or a collection ?
From now on, I this is my code :

$categories Mages:getModel('catalog/category');
$categories->load(1);
 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

All categories without regard to tree structure? Or only first level?

 
Magento Community Magento Community
Magento Community
Magento Community
 
lkbryant
Member
 
Avatar
Total Posts:  60
Joined:  2007-09-03
 

would you mind explaining both ways to do it?  the tree structure as well as just first level only?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

In Mage_Catalog_Block_Navigation you can see an example of building tree menu from category collection.

It is used in frontend/default/default/template/catalog/navigation/top.phtml

We are planning on implementing nested sets for categories to improve performance, so in future releases it might change.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

So, to get a list of all categories with all attributes without regard to tree structure should be something like:

$collection Mage::getResourceModel('catalog/category_collection')
  ->
addAttributeToSelect('*')
  ->
load();

After that $collection is an iterator object containing ‘catalog/category’ objects populated with all attributes.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Capitaine Commerce
Member
 
Avatar
Total Posts:  68
Joined:  2007-09-01
Lille, France
 

Thanks Moshe !
This is exactly what I wanted to know.
But now, what if I want the value of an attribute, let’s say the name.

$categories $collection->getItems();
while(list(,
$category) = each($categories)) {
     $attributes 
$category->getAttributes();
     while(list,
$attribute) = each($attributes)) {
          
echo $attribute->getName();
     
}
}

I get:
entity_type_id, attribute_set_id, created_at, etc....

I am able to get all attributes name, but I wonder how I can get their values. Can you help me ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Capitaine Commerce
Member
 
Avatar
Total Posts:  68
Joined:  2007-09-01
Lille, France
 

Okay, no need to answer. I eventually managed to get that damned attribute name. Here it goes :

($attributes is an array of Mage_Eav_Model_Entiry_Attribute class and $category is the Category Model Object)

$name $attributes['name'];

if(
$name->getResourceModel() {
     $value 
$name->getSource()->getOptionText($category->getData($name->getAttributeCode()));
else {
     $value 
$category->getData($name->getAttributeCode()),
}

echo $value;

I hope this is of interest for anyone. The same procedure can be used to get any attribute of a category or a product.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top