Quick script to mass enable categories in Magento

Just another easy one if you are looking to enable categories by ID - we usually incorporate this script into our product insertion API, so that it enables a category automatically for a product upon insertion. But it can also be used standalone. This script would be ideal alongside our export Magento category ID script
  • 1
    Create a CSV with a maximum of 1 column, with just the Magento category ID. Then save it to ./app/var/import/categoriesToEnable.csv. For examples sake, we will use,
    "1"
    "13"
    "15"
    
  • 2
    Copy the code below into a new file, ./quick_enableCats.php
    <?
    
     define('MAGENTO', realpath(dirname(__FILE__)));
     require_once MAGENTO . '/app/Mage.php';
     umask(0);
     Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 
     
     $file = fopen(MAGENTO . "/var/import/categoriesToEnable.csv", 'r');
     
     while (($line = fgetcsv($file)) !== FALSE) {
     $cats = explode(",",$line[1]);
     foreach ($cats as $cat) {
     $newcats[$cat] = true;
     }
     } 
    
     // Activate the categories it is in 
     foreach ($newcats as $cat=>$value) {
    
     if ($value && !empty($cat)) {
     
     try {
     
     $_category = Mage::getModel('catalog/category')->load($cat);
     
     if ($_category->getIsActive() === "0" || !$_category->getIsActive()) {
     $_category->setIsActive(1);
     $_category->save();
     echo "<br />Enabled ".$_category->getName();
     unset($_category);
     }
     
     } catch(Exception $e) {
     echo " Caught Cat Insert exception: ", $e->getMessage();
     }
     }
    
     }
     
    ?>
    
    
  • 3
    Visit the file above in your browser, or via command line. It runs very quickly (approx 500 categories per second), so don't be too surprised if its over quickly!
[syntaxhighlighter]