How I can get all categories and subcategories?

How I can get all categories and subcategories?

July 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How I can get all categories and subcategories?

Getting All Categories and Subcategories: A Comprehensive Guide

As an experienced Magento developer, I often encounter questions about retrieving all categories and subcategories from the system. One common challenge is ensuring that only active categories are displayed, even if the “Include in Navigation Menu” option is set to “No”. In this guide, I’ll walk you through a step-by-step solution to overcome this issue.

Understanding the Problem

The core problem arises when we want to get all categories, including their subcategories, regardless of whether they are included in the navigation menu. The default Magento code doesn’t handle this scenario effectively, leading to missing or incomplete category lists on the frontend.

The Incorrect Approach: Why it Fails

Let’s first analyze why the provided code snippet fails to solve the problem:

<?php
$_categories = Mage::getBlockSingleton('catalog/navigation');
foreach ($_categories->getStoreCategories() as $_category) {
    $category = Mage::getModel('catalog/category');
    $category->load($_category->getId());
    $subcategories = explode(',', $category->getChildren());

    // ...

}
?>

The main flaw in this approach is that it relies solely on the getStoreCategories() method, which only retrieves categories included in the navigation menu. If a category’s “Include in Nav Menu” option is set to “No”, it won’t be returned by this method.

Additionally, using $category->getChildren() doesn’t actually retrieve subcategories; it simply returns a comma-separated string of child category IDs. This requires manual parsing and loading of each individual subcategory, which can lead to performance issues and redundant database queries.

The Correct Solution: Retrieving Active Categories

To solve this problem, we need to directly query the catalog_category_entity table for active categories and their subcategories using Magento’s resource model. Here’s a more efficient approach:

<?php
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');

$query = "SELECT c.category_id, c.parent_id FROM catalog_category_entity AS c
          WHERE c.entity_id IS NOT NULL AND c.level > 0 AND c.is_active = 1";

$categories = $readConnection->fetchAll($query);

foreach ($categories as $category) {
    // Process each category and its subcategories here

    // ...

}
?>

In this solution, we directly query the catalog_category_entity table for all active categories (is_active = 1) with a level greater than 0 (i.e., not root categories). This approach ensures that both top-level and subcategories are retrieved.

Best Practices for Processing Categories

Once you have retrieved the desired categories, here are some best practices to follow:

  1. Use Magento’s Model: Instead of manually loading each category using $category->load($categoryId), use Mage::getModel('catalog/category')->load($categoryId) for better integration and consistency with Magento’s core functionality.

  2. Optimize Queries: If you need to retrieve a large number of categories, consider implementing pagination or filtering to reduce the load on your database.

  3. Handle Inactive Categories: Depending on your use case, you might want to handle inactive categories separately. You can modify the query to include both active and inactive categories by removing the c.is_active = 1 condition.

Conclusion

Retrieving all categories and subcategories in Magento requires a solid understanding of the core data models and resource management. By directly querying the catalog_category_entity table for active categories, you can overcome the limitations of using the getStoreCategories() method.

Remember to optimize your queries, use Magento’s built-in models, and handle inactive categories appropriately based on your application’s requirements. This approach ensures that you get a complete list of categories regardless of their navigation menu settings.

For more advanced techniques in Magento development, feel free to explore our Magento Development Guide at Laravel Company. We provide comprehensive tutorials and best practices for building efficient and secure e-commerce solutions.

Catalog Themes Errors

Our network