Magento get all children category follow store and parent category


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}/%"))
    ->addAttributeToFilter('is_active', 1)


Array return:

array(28) {
  array(11) {
    string(1) "4"
    string(1) "3"
    string(1) "3"
    string(1) "2"
    string(19) "2013-01-25 17:43:31"
    string(19) "2013-05-16 05:50:23"
    string(5) "1/2/4"
    string(1) "2"
    string(1) "2"
    string(1) "4"
    string(1) "1"


[“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();


Step 4: show to html

$categoryCount = count($arrListShow);
	//sort a-z
	foreach ($arrListShow as $item) {
		echo '<li><span><a href="'.$item['url'].'">"'.$item['name'].'"</a></span></li>';
} else {
	echo $this->__('No category!');


Leave a Reply

Your email address will not be published. Required fields are marked *