2

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

example

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

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

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

Code:

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

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

error_reporting(E_ALL);

require "app/Mage.php";

umask(0);

Mage::app('admin');

try{
    $category = Mage::getModel('catalog/category');
    $category->setName('Test category');
    $category->setUrlKey('Test category');
    $category->setIsActive(1);
    $category->setDisplayMode('PRODUCTS');
    $category->setStoreId(Mage::app()->getStore()->getId());
    $parentCategory = Mage::getModel('catalog/category')->load(1); // this is where you set parent category id
    $category->setPath($parentCategory->getPath());
    $category->save();
} catch(Exception $e) {
  // log
}
0

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->setId(null);
    $category->setData('name', $name);
    $category->setData('url_key', $name . '-catalog');
    $category->setData('display_mode', 'PRODUCTS');
    $category->setData('level', 1);
    $category->setData('store', $storeId);
    $category->getResource()->save($category);
    $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.