Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yogeshdubey2006/7c73b4d0e954e231f43da6f3138c2d6f to your computer and use it in GitHub Desktop.
Save yogeshdubey2006/7c73b4d0e954e231f43da6f3138c2d6f to your computer and use it in GitHub Desktop.
Magento 1 - Often times there is a need to change all Magento Categories from their default setting of 'Is Anchor = No' to 'Is Anchor=Yes'.
<?php
/* 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 category-achor-yes.php and place it in the root of your Magento install (where your index.php file is).*/
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();
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment