nileco-logo-small-text5
  • HOME
  • PROJECTS
  • SERVICES
  • ABOUT
    • WHO WE ARE
    • TESTIMONIALS
  • MAGENTO BLOG

Change all Categories in Magento to, Anchor is Yes, with 1 Click.

Magento How ToJune 23, 2014CJ

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.

Previous post How to Add Free Shipping Icon to Magento Product Page Next post Magento backorders and a custom message to add to In Stock/Out of Stock

10 comments. Leave new

Matt
February 26, 2015 2:20 pm

This post saved me a nice little chunk of tedious work. Thanks for sharing!

Reply
Darren
April 15, 2015 4:17 am

Excellent thank you

Reply
Nick
June 4, 2015 5:16 pm

This is great! Thanks.

Reply
k
September 2, 2015 7:01 am

Thanks for the code and it work.

Reply
k
September 2, 2015 7:02 am

Thanks it works perfectly.

Reply
Rich
September 10, 2015 7:30 pm

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.

Reply
Steve
April 10, 2016 1:21 pm

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!

Reply
Paul Johnson
July 27, 2016 7:38 am

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.

Reply
Reaze
September 16, 2016 12:08 pm

2016 and still helpful. Plus @Rich saved me too 🙂

Reply
Bryan Veloso
November 11, 2016 3:58 pm

this did the trick, saved me hours of manual labor! 😀

Reply

Leave a Reply Cancel reply

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

© 2016 All rights reserved. nileco