Magento support use with more other websites and other stores.
To work with category in each stores, in this post, I will guide for you get categories with ID store and ID root category.
Here I will create an array include all categories, don’t sort follow level.
Step 1: get ID store and ID root category relative
$storeId = Mage::app()->getStore()->getId(); //get current store ID $categoryRootId = Mage::app()->getStore($storeId)->getRootCategoryId(); //get root category follow current store
Step 2: get ID all categories of root category
/*get all info primary of all children categories in root category*/ $_categories = Mage::getModel('catalog/category')->getCollection() ->addFieldToFilter('path', array('like' => "%/{$categoryRootId}/%")) ->addAttributeToSelect('entity_id') ->addAttributeToFilter('is_active', 1) ->getData();
Array return:
array(28) { [0]=> array(11) { ["entity_id"]=> string(1) "4" ["entity_type_id"]=> string(1) "3" ["attribute_set_id"]=> string(1) "3" ["parent_id"]=> string(1) "2" ["created_at"]=> string(19) "2013-01-25 17:43:31" ["updated_at"]=> string(19) "2013-05-16 05:50:23" ["path"]=> string(5) "1/2/4" ["position"]=> string(1) "2" ["level"]=> string(1) "2" ["children_count"]=> string(1) "4" ["is_active"]=> string(1) "1" } [1]=>... [2]=>... ... }
Here:
[“entity_id”]: ID child category
[“parent_id”]: ID of parent category
[“path”]: paths to that category
[“level”]: lever of category
[“children_count”]: count child categories
Step 3: get name, url,… and set to an array
/*get name and url of categories in below array*/ $arrListShow = array(); if(count($_categories)) { $j = 0; foreach($_categories as $childCate) { $childCategoryId = Mage::getModel('catalog/category')->load($childCate['entity_id'])->getChildren(); $arrCategoryId = explode(',', $childCategoryId); $i = 0; foreach ($arrCategoryId as $idCate) { $cate = Mage::getModel('catalog/category')->load($idCate); if($cate['is_active'] == 1) { $arrListShow[$j][$i]['name'] = ucwords($cate->getName()); $arrListShow[$j][$i]['url'] = $cate->getUrl(); $i++; } } $j++; } }
Step 4: show to html
/*output*/ $categoryCount = count($arrListShow); if($categoryCount){ //sort a-z asort($arrListShow); foreach ($arrListShow as $item) { echo '<li><span><a href="'.$item['url'].'">"'.$item['name'].'"</a></span></li>'; } } else { echo $this->__('No category!'); }