Often times there is a need to change all Magento Categories from their default setting of ‘Is Anchor = No’ to ‘Is Anchor=Yes’. Making the change to an Anchor Category activates the Layered Navigation, also known as filtered navigation. If you have multiple Categories this can be a time-consuming task to do manually.
The better way is to use the following code to run through and change all Categories to Anchor in just 1 Click.
*It is always a good idea before making changes to the database that you first create a database backup. This has become very quick and easy using the Magento Admin. To make your backup using Magento Admin, navigate to [System] [Tools] [Backups], once there select the option Database Backup (don’t worry it doesn’t take long).
Now that you are ready, create a new file called all2anchor.php and place it in the root of your Magento install (where your index.php file is). In this new file paste the following:
<? error_reporting(E_ALL); ini_set('display_errors', '1'); // Load Up Magento Core define('MAGENTO', realpath('')); 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(); }
After saving the file ‘all2anchor.php’ you can run it by using the address of your store followed by /all2anchor.php. www.yourstore/all2anchor.php
That’s it you’re done! Now remove this file and continue with developing your Magento store.
10 comments. Leave new
This post saved me a nice little chunk of tedious work. Thanks for sharing!
Excellent thank you
This is great! Thanks.
Thanks for the code and it work.
Thanks it works perfectly.
This didn’t work for me (1.9.2.1) to fix it I needed to define the store ID (In my case ‘0’).
Mage::app()->setCurrentStore(0);
This it worked fine.
Superbly useful snippet! Kept for future use. Well done a massive time saver to anyone with Magento and a ton of categories that are not anchors!
Thank you! You’re a life saver! I was otherwise going to have to do this manually or by a SQL command for 300+ categories.
I did have to do what Rich said and define my store.
2016 and still helpful. Plus @Rich saved me too 🙂
this did the trick, saved me hours of manual labor! 😀