4

I'm trying to get all parent categories except root category from the collections returned using Mage::getModel('catalog/category');

My code :

$_category = Mage::getModel('catalog/category');    
    $_categories = $_category
                    ->getCollection()
                    ->addAttributeToSelect(array('name', 'image', 'description'));

    foreach ($_categories as $_category){
        print_r($_category);
        die(0);
        echo $_category->getName().'<>';

}

Output is :

Mage_Catalog_Model_Category Object (
    [_eventPrefix:protected] => catalog_category 
    [_eventObject:protected] => category 
    [_cacheTag:protected] => catalog_category 
    [_useFlatResource:protected] => 
    [_designAttributes:Mage_Catalog_Model_Category:private] => Array (
        [0] => custom_design 
        [1] => custom_design_from 
        [2] => custom_design_to 
        [3] => page_layout 
        [4] => custom_layout_update 
        [5] => custom_apply_to_products
    ) 
    [_treeModel:protected] => 
    [_defaultValues:protected] => Array () 
    [_storeValuesFlags:protected] => Array () 
    [_lockedAttributes:protected] => Array () 
    [_isDeleteable:protected] => 1 
    [_isReadonly:protected] => 
    [_resourceName:protected] => catalog/category 
    [_resource:protected] => 
    [_resourceCollectionName:protected] => catalog/category_collection 
    [_dataSaveAllowed:protected] => 1 
    [_isObjectNew:protected] => 
    [_data:protected] => Array (
        [entity_id] => 1 
        [entity_type_id] => 3 
        [attribute_set_id] => 0 
        [parent_id] => 0 
        [created_at] => 2013-09-14 12:39:35 
        [updated_at] => 2013-09-14 12:39:35 
        [path] => 1 
        [position] => 0 
        [level] => 0 
        [children_count] => 45 
        [name] => Root Catalog
    ) 
    [_hasDataChanges:protected] => 1 
    [_origData:protected] => Array (
        [entity_id] => 1 
        [entity_type_id] => 3 
        [attribute_set_id] => 0 
        [parent_id] => 0 
        [created_at] => 2013-09-14 12:39:35 
        [updated_at] => 2013-09-14 12:39:35 
        [path] => 1 
        [position] => 0 
        [level] => 0 
        [children_count] => 45 
        [name] => Root Catalog
    ) 
    [_idFieldName:protected] => entity_id 
    [_isDeleted:protected] => 
    [_oldFieldsMap:protected] => Array () 
    [_syncFieldsMap:protected] => Array ()
)

May be I need to add an extra attribute for this? Any other idea please share

  • what do you mean by "all parent categories"? All categories that have children? all categories on the first level? all parent categories of a specific category? – Marius Sep 15 '13 at 8:37
  • @Marius All categories on the first level.. :-) – ravisoni Sep 15 '13 at 9:46
23

This should get you the first level categories

$categories = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect('*')//or you can just add some attributes
    ->addAttributeToFilter('level', 2)//2 is actually the first level
    ->addAttributeToFilter('is_active', 1)//if you want only active categories
;
  • Great.. I never thought about addAttributeToFilter('level', 2) magic filter. Thanks – ravisoni Sep 15 '13 at 10:11
2

I suspect you are asking for all the 'Top Level' categories here? thus the children of the root category only, and not their children?

You can use the category TreeModel to get this done (or to get to categories, using a categoryID as the starting (top level node)

As an example to get you started:

$treeModel = Mage::getModel('catalog/category')->getTreeModel()->loadNode($topLevelCatId);
$nodes = $treeModel->loadChildren()->getChildren();

If you are looking for only categories that have child categories, the same code can be used, but you need to iterate $nodes, and check for existence of child categories.

  • How to get the name of category from node object ? Debugged it, couldn't find. – Hashid May 3 '15 at 15:03
0

For the people looking for MySQL query to fetch all Magento categories.

SELECT
    e.entity_id AS id,
    e.parent_id,
    e.path,
    e.`level`,

IF (
    at_name.value_id > 0,
    at_name.
VALUE
    ,
    at_name_default.
VALUE

) AS `name`
FROM
    `catalog_category_entity` AS `e`
INNER JOIN `catalog_category_entity_varchar` AS `at_name_default` ON (
    `at_name_default`.`entity_id` = `e`.`entity_id`
)
AND (
    `at_name_default`.`attribute_id` = '41'
)
LEFT JOIN `catalog_category_entity_varchar` AS `at_name` ON (
    `at_name`.`entity_id` = `e`.`entity_id`
)
AND (
    `at_name`.`attribute_id` = '41'
)
0

If you want only top level, than you can go with:

$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
foreach($_categories as $_category)
  {
   echo $_category->getName();
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.