Magento create category programmatically


Magento create category programmatically : You can create categories programatically in magento. Mage::getModel(‘catalog/category’); is used to initialize and create the category in magento. You can set category url, name, parent category and path etc in the category. Here is simple example of creating category programatically.


Magento create category programmatically

Here is syntax to create category programmatically in magento-

Here is syntax to create category programmatically in magento

 public function createCategory(){
    $parentId = 12;// id of parent category 
    $category = Mage::getModel('catalog/category');
    $category->setName('My Category');
    $category->setUrlKey('my-category');
    $category->setIsActive(1); // to make active
    $category->setDisplayMode('PRODUCTS');
    $category->setIsAnchor(1); // This is for active anchor
    $category->setStoreId(Mage::app()->getStore()->getId());
    $parentCategory = Mage::getModel('catalog/category')->load($parentId);
    $category->setPath($parentCategory->getPath());
    $category->save();

}

Which will create the category under the parent category with id 12.

Magento Set Path of category | Parent Category

You can set path of a category in magento as below –

Magento Set Path of category | Parent Category Example

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

Advertisements

Add Comment

📖 Read More