Programmatically get Root Categories by Store Id in Magento

Programmatically get Root Categories by Store Id in Magento

In this post we will show cod for how to Programmatically get Root Categories by Store Id in Magento

By following code we can Programmatically get product grouped detail by id in magento

// pass store id
$store_id = 1;
// set categories result array
$result_store_categories = array();

$categories_total = 0;
try 
{
	// set store id 
    $store_root_category_id = Mage::app()->getStore($store_id)->getRootCategoryId();
	
	// set root category detail
    $store_category_collection = Mage::getModel('catalog/category')->load($store_root_category_id);
	
	// get detail of 'name', 'image', 'description','store'
    $store_categories_result = $store_category_collection->getCollection()
->addAttributeToSelect(array('name', 'image', 'description','store'))
->addIdFilter($store_category_collection->getChildren());
	// get detail of categories 
    foreach($store_categories_result as $current_category_val)
	{
		// set value in array 
		$result_store_categories["categories"][$categories_total] = array(
		    "id" => $current_category_val->getId(),
		    "name" => $current_category_val->getName(),
		    "image" => Mage::getModel('catalog/category')->load($current_category_val->getId())->getImageUrl(),
		);
		// incrise count of total
		$categories_total = $categories_total + 1;
    }
	// set total count as index
    $result_store_categories["total"] = $categories_total;
}
catch(Exception $exception) 
{
	// print exception info.
	print_r($exception);
}

// print result (Root Categories by Store Id)
echo "</pre>";
print_r($result_store_categories);
echo "</pre>";

Leave a Comment

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

13  +    =  19

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US