Get all categories
-
13-12-2019 - |
Question
There are a few answers related to this but the problem I am facing is a little different. I'm only getting categories that are set as visible in Navigation Menu
It happens, for example, when I try to get all categories I have to show them with Mage::helper('catalog/category')->getStoreCategories();
It seemed to give me all categories but it didn't. There are like 10 main categories that I don't include in Navigation Menu with that option to avoid too many categories on the top of the page. Instead, I have an static cms named more
where I add those 10 manually at the end of the navigation menu.
So, what I'm asking is if there is a way to get all categories or another way to add these 10 categories in a single menu tab with the navigation menu activated.
Solution
Copy this code on your magento root dir and run it from url.
<html>
<body>
<?php
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
ini_set('memory_limit', '600M');
ini_set('max_execution_time', 1800);
umask(0);
Mage::app('admin');
?>
<?php
$cat = Mage::getModel('catalog/category')->load(2);
$subcats = $cat->getChildren();
$products_row = array();
foreach(explode(',',$subcats) as $subCatid)
{
$data = array();
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
$data[] = $_category->getName();
$data[] = str_replace('/getCat.php','',$_category->getURL());
$sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
$sub_subcats = $sub_cat->getChildren();
foreach(explode(',',$sub_subcats) as $sub_subCatid)
{
$_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
if($_sub_category->getIsActive()) {
$data[] = $_category->getName().' >> '.$_sub_category->getName();
$data[] = str_replace('/getCat.php','',$_sub_category->getURL());
$sub_sub_cat = Mage::getModel('catalog/category')->load($sub_subCatid);
$sub_sub_subcats = $sub_sub_cat->getChildren();
foreach(explode(',',$sub_sub_subcats) as $sub_sub_subCatid)
{
$_sub_sub_category = Mage::getModel('catalog/category')->load($sub_sub_subCatid);
if($_sub_sub_category->getIsActive()) {
$data[]= $_category->getName().' >> '.$_sub_category->getName().' >> '.$_sub_sub_category->getName();
$data[] = str_replace('/getCat.php','',$_sub_sub_category->getURL());
}
}
}
}
}
$products_row[] = $data;
}
echo '<pre>';
print_r($products_row);
echo '<pre>';
?>
</body>
</html>
OTHER TIPS
Try this. It will load all the categories even if they are not visible in the main menu.
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter();
foreach($categories as $cat) {
echo $cat->getName() . "<br>";
}