2

I need to create more then 50+ category. I Tried manually it take a too much of time so how to create category programatically?

4 Answers 4

6

Try This one:

<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
$parentId = '2';
 try{
    $category = Mage::getModel('catalog/category');
    $category->setName('your cat name');
    $category->setUrlKey('your-cat-url-key');
    $category->setIsActive(1);
    $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->save();
} catch(Exception $e) {
    print_r($e);
}
?>
4
  • Where i have to use this
    – Magento 2
    Commented Oct 1, 2015 at 7:36
  • copy code in a file and place it in your magento root dir and run from url like: www.yoursite.com/codefile.php
    – Arunendra
    Commented Oct 1, 2015 at 7:38
  • it's working fine
    – Magento 2
    Commented Oct 1, 2015 at 8:04
  • i'm getting error like "Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails" Commented Mar 3, 2020 at 9:18
0

For the bulk import categories you can use below extension.

http://www.magentoworks.net/magento-extension/

Or You can follow below link which provide script to import categories using CSV.

https://www.sonassi.com/knowledge-base/quick-script-batch-create-magento-categories/

0

In order to fulfill,your requirement,you can use php shell script:

Write php script at magento root dir.

Using magento this you can Create category quickly :

require_once 'app/Mage.php';
Mage::app("admin");
try{
    $category = Mage::getModel('catalog/category');
    $category->setName('check');
    $category->setUrlKey('new-category');
    $category->setIsActive(1);
    $category->setDisplayMode(Mage_Catalog_Model_Category::DM_PRODUCT);
    $category->setIsAnchor(1); //for active achor
    $category->setStoreId(Mage::app()->getStore()->getId());
    $parentCategory = Mage::getModel('catalog/category')->load($parentId);
    $category->setPath($parentCategory->getPath());
    $category->save();
} catch(Exception $e) {
    var_dump($e);
}

Or,you can use any 3-rd partly extension like

magentoworks

http://www.magentocommerce.com/magento-connect/import-export-categories-extension.html

or follow below blog:

sonassi

5
  • Where i have to use this
    – Magento 2
    Commented Oct 1, 2015 at 7:14
  • 1
    i tried in your code to my root folder but it showing blank page
    – Magento 2
    Commented Oct 1, 2015 at 7:31
  • If it's showing a blank page it means the import was complete if you login to magento you should now see a category called check
    – rob3000
    Commented Oct 1, 2015 at 7:34
  • No i checked it's not creating
    – Magento 2
    Commented Oct 1, 2015 at 7:38
  • after running the above script, im my admin i checked and a blank row got created without the name of the category
    – Sushivam
    Commented Feb 8, 2017 at 9:59
0
/* supply parent id */

$parentId = '10';

$category = new Mage_Catalog_Model_Category();
$category->setName('Storybloks');
$category->setUrlKey('new-category');
$category->setIsActive(1);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(0);

$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());               

$category->save();
unset($category);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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