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!
  • Mike

    This didn’t work for me in ver. 1.4.0.1. can it also be done with mysql?

    Thanks,

    Mike

  • Mike

    Am getting the errors:

    ./quick_enableCats.php: line 1: ?: No such file or directory
    ./quick_enableCats.php: line 3: syntax error near unexpected token `’MAGENTO’,’
    ./quick_enableCats.php: line 3: ` define(‘MAGENTO’, realpath(dirname(__FILE__)));’

    Strange…

    Mike

  • https://www.sonassi.com ben@sonassi.com

    That doesn’t look like a 1.4 specific error – it just looks like php shortags are not enabled on your system.

  • http://www.foowebs.com Kim Young

    ok – i ran this as instructed, but the categories are not adding the ids as instructed but autoassigning others, and the value (name) from the export function isnt carrying through. says it worked though… any thoughts?

  • http://thingtronics.com Mishi

    This wasn’t working for me until I changed

    $cats = explode(“,”,$line[1]);

    to

    $cats = explode(“,”,$line[0]);

    That appears to have done the trick – thanks for saving me hours of finger-breaking work!

    I’m on 1.4.1.1.

  • http://twitter.com/hd_ng Tom Harding

    Every time I’ve had a category related problem, I’ve somehow stumbled across solutions via you guys (category import/export and now this). You guys are geniuses, thankyou very much! :-)