Import categories magento Programmatically

This tutorial explains how to read the data from the CSV file and import categories magento Programmatically.

This program reads the data from the CSV (fig 1) and create the categories as (fig 2) shown below.Please click here to download categories.csv

This tutorial is based on Magento version 1. Please click here to refer the tutorial which explains how to import categories in magento 2 programmatically.

fig 1
magento import categories

fig 2
Import magento categories

Step by step explanation

  1. Read CSV data
  2. Import the category

Read CSV data

As the first step it reads the data from the CSV file .The result were saved in an array ($arrResult)

function getProducts()
    $file = 'csv/products.csv';
    $arrResult = array();
    $headers = false;
    $handle = fopen($file, "r");
    if (empty($handle) === false) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            if (!$headers) {
                $headers[] = $data;
            } else {
                $arrResult[] = $data;
    return $arrResult;

Import categories magento

The below image show the content of the $arrResult.
magento categories
This step iterate through the above array and create the categories and subcategories . Save the category images in the media/catalog/category/data folder.

function importCategories($arrResult)

    Mage::register('isSecureArea', 1);
    $parentId = '2';
    $list = array();

    foreach ($arrResult as $import_category) {
        try {
            if (strtolower($import_category[16]) == 'true') {
                $enabled = 1;
            } else {
                $enabled = 0;

            if ($import_category[1] == 0) {
                $parentId = '2';
            else {
                $parentId = $list[$import_category[1]];

            $category = Mage::getModel('catalog/category');
            $category->setIsAnchor(1); //for active anchor
            $parentCategory = Mage::getModel('catalog/category')->load($parentId);
            $list[$import_category[0]] = $category->getId();
            echo 'Category ' . $category->getName() . ' ' . $category->getId() . ' imported successfully' . PHP_EOL;
        } catch (Exception $e) {
            echo 'Something failed for category ' . $import_category[2] . PHP_EOL;
    return $list;


Save Category Id

The new category ids are used for importing the product and to assign the products to new categories. The importCategories() function save the new categories ids in the $list array and set the old category Id as the index ($list[$import_category[0]] = $category->getId();).

Please click here to refer the tutorial which explains how to import categories in magento 2 programmatically.

Share this Tutorial

2 comments on “Import categories magento Programmatically

  1. Shawn on

    Hello, I am a not sure how to “run” this code. Is it supposed to be run as a PHP file? I copied the code, added to the beginning and end, and ran it on my server where Magneto lives, but nothing happens. Could you provide more detail, please? Thank you!

    • pearl on

      please using command prompt or ssh to your server using the ftp username and password . Then browse to your folder which contains the file and run the command php filename.php




Leave a Reply

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