I need to create more then 50+ category. I Tried manually it take a too much of time so how to create category programatically?
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);
}
?>
-
-
copy code in a file and place it in your magento root dir and run from url like: www.yoursite.com/codefile.php – Arunendra Oct 1 '15 at 7:38
-
-
i'm getting error like "Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails" – prashanthi kontemukkala Mar 3 '20 at 9:18
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/
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
http://www.magentocommerce.com/magento-connect/import-export-categories-extension.html
or follow below blog:
-
-
1
-
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 Oct 1 '15 at 7:34 -
-
after running the above script, im my admin i checked and a blank row got created without the name of the category – Sushivam Feb 8 '17 at 9:59
/* 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);