Magento Create Category Programmatically

Magento Create Category Programmatically :

You can create categories programmatically in Magento by Mage::getModel(‘catalog/category’) method which is used to initialize and create the category in Magento 1.9


Note : There should be a parent category to begin this process.

Magento Create Category Programmatically Syntax

This code can be placed inside any PHP file that calls Magento bootstrapper and will execute the functionality accordingly.
 

<?php
$mageFilename = 'app/Mage.php';
$parentId = '2';// this is id of parent category
 try{
    $category = Mage::getModel('catalog/category');
    $category->setName('category name');
    $category->setUrlKey('category-url-key');
    $category->setIsActive(1);
    $category->setDisplayMode('PRODUCTS');
    $category->setIsAnchor(1); //active anchor
    $category->setStoreId(Mage::app()->getStore()->getId());
    $parentCategory = Mage::getModel('catalog/category')->load($parentId);
    $category->setPath($parentCategory->getPath());
    $category->save();
} 
?>

Using this code one can easily create 50+ categories in Magento programmatically. You can add category title and URL key to show in the front-end respectively.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *