Get a list of all magento categories and their id's on screen when I visit a php file

Get a list of all magento categories and their id's on screen when I visit a php file

August 25, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get a list of all magento categories and their id's on screen when I visit a php file

Title: Retrieving Magento Categories and IDs Using PHP

Magento provides a robust framework for managing your online store’s product categories effectively. In this post, we will guide you through creating a simple PHP script that retrieves all the available categories along with their corresponding IDs when accessed via a URL like http://yourdomain.com/category.php.

Understanding the Code

Firstly, let’s break down the provided code:

$category = Mage::getModel('catalog/category');
$catTree = $category->getTreeModel()->load();
$catIds = $catTree->getCollection()->getAllIds();
$cats = array();

if ($catIds) {
    foreach ($catIds as $id) {
        $cat = Mage::getModel('catalog/category');
        $cat->load($id);
        $cats[$cat->getId()] = $cat->getName();
    }
}

This script does the following:

  1. It retrieves a category model using Mage::getModel('catalog/category').
  2. Loads the category tree with all categories and their IDs.
  3. Retrieves an array of all category IDs.
  4. Iterates through each ID, loads the corresponding category object, and stores its name associated with its ID in the $cats array.

Placing the Script

To get this script working, you’ll need to place it within your Magento installation directory, typically found at public_html/magento. Here are the steps:

  1. Navigate to the root of your Magento installation via FTP or SSH.
  2. Create a new PHP file named category.php in the root directory (alongside files like .htaccess, index.php, etc.).

Including Mage

For the script to work correctly, you must include the Magento core file called “Mage.php”. Add this line at the beginning of your category.php:

$mageFilename = 'app/Mage.php';
require_once $mageFilename;

This will load the necessary Magento classes and enable the use of its models.

Complete Script

Your complete category.php should now look like this:

<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;

$category = Mage::getModel('catalog/category');
$catTree = $category->getTreeModel()->load();
$catIds = $catTree->getCollection()->getAllIds();
$cats = array();

if ($catIds) {
    foreach ($catIds as $id) {
        $cat = Mage::getModel('catalog/category');
        $cat->load($id);
        $cats[$cat->getId()] = $cat->getName();
    }
}
?>

Accessing the Script

Once you’ve saved your changes, you can access this script by entering http://yourdomain.com/category.php in your web browser. You should now see a list of all categories along with their respective IDs.

If you encounter a blank screen instead, double-check that:

  1. Your Magento installation is active and accessible.
  2. The category.php file is placed correctly within the root directory.
  3. There are no typos or syntax errors in your script.

Remember to remove this script once you’ve finished testing it, as exposing sensitive data like category IDs can potentially pose security risks if left unprotected.

We hope this guide has clarified the process of retrieving Magento categories and IDs using PHP. For more advanced customizations and tutorials on Magento development, feel free to explore our blog at LaravelCompany.

Catalog CLI

Our network