category – How to Alphabetically Sort Multiple Root Categories

category

I am successfully using the code provided by Marius (thank you Marius) to sort categories alphabetically.

Categories – how to display in alphabetical order? (Programmatically)

<?php
//set error reporting to see if something is wrong.
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
//set developer mode for additional debug functionality
Mage::setIsDeveloperMode(true);
//instantiate Mage_Core_Model_App;
Mage::app();
//set the initial parameters for the function
$root = 1;
$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
//sort categories
orderCategories($root, $conn);

function orderCategories($parentId, $conn){
    //The position field is in the main category table
    //add prefix if you have one
    $table = 'catalog_category_entity';
    //get the children for a specific category id
    $collection = Mage::getModel('catalog/category')->getCollection()
        ->addAttributeToSelect('name')
        ->addFieldToFilter('parent_id', $parentId);
    $categories = array();
    foreach ($collection as $category){
        //in case there are 2 categories with the same name we shouldn't skip them
        //but there shouldn't be
        $categories[$category->getName().'_'.$category->getId()] = $category->getId();
    }
    //sort categories by name
    ksort($categories);
    //set their position
    $position = 1;
    foreach ($categories as $name=>$id){
        $q = "UPDATE {$table} SET `position` = {$position} where `entity_id` = {$id}";
        $conn->query($q);
        $position++;
        //sort the current category children by calling the same function recursively
        orderCategories($id, $conn);
    }
}

I haven't modified the code at all and it works perfectly, but only on the first root category. I have multiple stores, each with it's own root category, so I would like to modify the code to work on all root categories or, preferably, be able to nominate an individual root category for sorting. Any help with achieving this will be much appreciated.

Edit: I have tried changing the value of $root to different category IDs (eg. $root = 5;) but it doesn't make any difference. Is category ID the correct value to be using for $root if I want to specify the root category for this function?

Best Answer

Please use bellow code :

<?php
require_once('app/Mage.php');
Mage::app('default');

class AR
{
    public function index()
    {
        $this->sortOrderRootCategories();
    }

    public function sortOrderRootCategories()
    {
        $conn = Mage::getSingleton('core/resource')->getConnection('core_write');
        //The position field is in the main category table
        //add prefix if you have one
        $table = 'catalog_category_entity';
        //get the children for a specific category id
        $collection = Mage::getModel('catalog/category')->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('level', 1)
                ->addAttributeToFilter('is_active', 1);
        $categories = array();
        foreach ($collection as $category){
            //in case there are 2 categories with the same name we shouldn't skip them
            //but there shouldn't be
            $categories[$category->getName().'_'.$category->getId()] = $category->getId();
        }
        //sort categories by name
        ksort($categories);
        //set their position
        $position = 1;
        foreach ($categories as $name=>$id){
            $q = "UPDATE {$table} SET `position` = {$position} where `entity_id` = {$id}";
            $conn->query($q);
            $position++;
            $this->orderCategories($parentId);
            //sort the current category children by calling the same function recursively
        }
    }

    public function orderCategories($parentId)
    {
        $conn = Mage::getSingleton('core/resource')->getConnection('core_write');

        //The position field is in the main category table
        //add prefix if you have one
        $table = 'catalog_category_entity';
        //get the children for a specific category id
        $collection = Mage::getModel('catalog/category')->getCollection()
            ->addAttributeToSelect('name')
            ->addFieldToFilter('parent_id', $parentId);
        $categories = array();
        foreach ($collection as $category){
            //in case there are 2 categories with the same name we shouldn't skip them
            //but there shouldn't be
            $categories[$category->getName().'_'.$category->getId()] = $category->getId();
        }
        //sort categories by name
        ksort($categories);
        //set their position
        $position = 1;
        foreach ($categories as $name=>$id){
            $q = "UPDATE {$table} SET `position` = {$position} where `entity_id` = {$id}";
            $conn->query($q);
            $position++;
            //sort the current category children by calling the same function recursively
            $this->orderCategories($parentId);
        }
    }
}

$obj = new AR();
$obj->index();
?>
Related Topic