Set All Your Magento Categories to is_anchor

I recently was tasked to set every category inside our Magento installation have its is_anchor attribute set to true. We have over 600 categories so it would be a huge task to do this by hand so I put together a quick script that did this in less than a minute.

error_reporting(E_ALL);
ini_set('display_errors', '1');

// Load Up Magento Core
define('MAGENTO', realpath('/path/to/magento'));

require_once(MAGENTO . '/app/Mage.php');

$app = Mage::app();

$categories = Mage::getModel('catalog/category')
 ->getCollection()
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('is_anchor', 0)
 ->addAttributeToFilter('entity_id', array("gt" => 1))
 ->;setOrder('entity_id')
 ;

foreach($categories as $category) {
 echo $category->getId() . "\t" . $category->getName() . "\n";
 $category->setIsAnchor(1);
 $category->save();
}

The first thing I did was pull in Magento’s functionality so I could load up a collection of categories. You will notice that I only got categories that had their is_anchor property set to 0 and an entity_id greater than 1.

From there I did a foreach on all the categories and set the category is_anchor property to 1 and saved it. I decided to echo out the category name so I could watch the script process the categories.

Leave a Reply

Your email address will not be published. Required fields are marked *