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,
  • 2
    Copy the code below into a new file, ./quick_enableCats.php
     $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()) {
     echo "
    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. can it also be done with mysql?



  • 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__)));'



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

  • 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?

  • This wasn't working for me until I changed

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


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

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

    I'm on

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

  • Merrill

    As a standalone solution for mass operations over categories and products in Magento there is extension Store Manager with simple and convenient Category Management feature which allows you to easily enable, modify, move, or remove Categories, and adjust all tree structure and sub-structure.