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!
Last 5 posts in Magento
MySQL Limitations on the Flat Catalogue in Magento
Like our standard Magento Wordpress integration? Try the DELUXE version!
Split your Magento sitemap into smaller chunks
Mass delete products in Magento
Catalog Search Index refresh running slow or halting/freezing


Mike
11 Mar. 2010
This didn’t work for me in ver. 1.4.0.1. can it also be done with mysql?
Thanks,
Mike
Mike
11 Mar. 2010
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
ben@sonassi.com
12 Mar. 2010
That doesn’t look like a 1.4 specific error – it just looks like php shortags are not enabled on your system.
Kim Young
17 Aug. 2010
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?