Import the category programmatically

Import the magento category programmatically

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;

}

Share this Tutorial