"Add Root Category" programmatically in magento 1.6

To add root category use model "Mage_Catalog_Model_Category". You will need to specify Category Name, Path, Is Active, etc.

Category Name - Name of you cagegory e.g. Electornics.

Path - For root category it will be 1.

Is Active - If 1 then category is active on site. If 0 then category is hide on site.

Display Mode - If set PRODUCTS then displays the products assigned to this category in grid/list format.

You can use following script to add root category in magento v1.6.

  $categoryName = "Electronics"; // Name of Category

  // Create category object
  $category = Mage::getModel('catalog/category');
  $category->setStoreId(0); // No store is assigned to this category

  $rootCategory['name'] = $categoryName;
  $rootCategory['path'] = "1"; // this is the catgeory path - 1 for root category
  $rootCategory['display_mode'] = "PRODUCTS";
  $rootCategory['is_active'] = 1;

  $category->addData($rootCategory);

  try {
    $category->save();
    $rootCategoryId = $category->getId();
  }
  catch (Exception $e){
    echo $e->getMessage();
  }

You can also specify some other attribute with root category.

Is Anchor - If set to 1 then its sub-categories will display as layered navigation options.

Category description, meta title, meta keywords, etc.

  // Create category object
  $category = Mage::getModel('catalog/category');
  $category->setStoreId(0); // No store is assigned to this category. 

  $rootCategory['name'] = $categoryName;
  $rootCategory['path'] = "1"; // this is the catgeory path - 1 for root category. 
  $rootCategory['description'] = "Category Description";
  $rootCategory['meta_title'] = "Meta Title";
  $rootCategory['meta_keywords'] = "Meta Keywords";
  $rootCategory['meta_description'] = "Meta Description";
  $rootCategory['display_mode'] = "PRODUCTS";
  $rootCategory['is_active'] = 1;
  $rootCategory['is_anchor'] = 1;

  $category->addData($rootCategory);

  try {
    $category->save();
    $rootCategoryId = $category->getId();
  }
  catch (Exception $e){
    echo $e->getMessage();
  }

Created At: 22 November, 2024

Views: 9,027



Social Sharing
Search