I want to create a Root Category programatically, i am using this function but category is created under magento "Default Category".


  • I have put reopen vote for this question. I Guss that it have enough info – Amit Bera Sep 22 '15 at 4:32

Whenever,you want to create a root category then must set

  • Store id as 0
  • Parent id(parent) set as 1.


$storeId    = 0;
$category = Mage::getModel('catalog/category');
$category->setName('Test category');
$category->setUrlKey('Test category');
$parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
  • Working correctly.... – Mathew Sep 22 '15 at 4:31

If you set correct parent category ID, it should work. Try category ID 1 for parent category ID.


require "app/Mage.php";



    $category = Mage::getModel('catalog/category');
    $category->setName('Test category');
    $category->setUrlKey('Test category');
    $parentCategory = Mage::getModel('catalog/category')->load(1); // this is where you set parent category id
} catch(Exception $e) {
  // log

This should work I guess

 * creates a new category for store-ID with the data of the template category.
 * @param int    $template ID of the category to clone from
 * @param string $name     of the category to add
 * @return int ID of the newly created category
 * @throws RuntimeException
protected function _addRootCategory($template, $name)
    $storeId = Mage_Core_Model_App::ADMIN_STORE_ID;

    /** @var $category Mage_Catalog_Model_Category */
    $category = Mage::getModel('catalog/category')->load($template);
    if (!$category->getId()) {
        throw new RuntimeException(sprintf('Failed to load template category by id %d', $template));

    $category = clone $category;
    $category->setData('name', $name);
    $category->setData('url_key', $name . '-catalog');
    $category->setData('display_mode', 'PRODUCTS');
    $category->setData('level', 1);
    $category->setData('store', $storeId);
    $categoryId = $category->getId();
    if (!$categoryId) {
        throw new RuntimeException(
            sprintf('Failed to create new root category "%s" for store ID %d', $name, $storeId)

    $parentId = 1;
    $category->move($parentId, null);

    return $categoryId;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.