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 getCategories()
{
    $file = 'csv/categories.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;
            }
        }
        fclose($handle);
    }
    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->setName($import_category[2]);
            $category->setMetaTitle($import_category[2]);
            $category->setIncludeInMenu(1);
            $category->setUrlKey($import_category[10]);
            $category->setDescription(strip_tags($import_category[11]));
            $category->setMetaDescription($import_category[12]);
            $category->setMetaKeywords($import_category[13]);
            $category->setIsActive($enabled);
            $category->setDisplayMode('PRODUCTS');
            $category->setIsAnchor(1); //for active anchor
            $category->setStoreId(Mage::app()->getStore()->getId());
            $parentCategory = Mage::getModel('catalog/category')->load($parentId);
            $category->setPath($parentCategory->getPath());
            $category->setCustomUseParentSettings(true);
            $category->setImage($import_category[6]);
            $category->save();
            $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;
            print_r($e);
        }
    }
    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!

    Reply
    • 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

      Thanks

      Liz

      Reply

Leave a Reply

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

*