2

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?

3 Answers 3

1

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();
?>
10
  • Thanks for your answer Abdul. Looks good but I got the following error Fatal error: Call to undefined function orderCategories() in /home/captai22/devsite/categories.php on line 16 which makes me think that the top half of the code needs modifying also. I tried replacng
    – bigbadbaz
    Nov 9, 2015 at 10:48
  • have you added old code also same file?
    – Abdul
    Nov 9, 2015 at 10:53
  • my code only for the multiple root categories sort order and orderCategories() is different function
    – Abdul
    Nov 9, 2015 at 10:55
  • Oh so just add your code to the same file? OK I'll try that thanks :)
    – bigbadbaz
    Nov 9, 2015 at 10:56
  • Sadly it didn't work. I took your code Abdul and put it below Marius' code (provided in my question), ran it, reindexed, and there was no change to the categories order. And there were no errors either so I can't provide any more info I'm afraid. Any suggestions? I really appreciate your help.
    – bigbadbaz
    Nov 9, 2015 at 11:14
0

I didn't test it but this collection might give you all category collection with sorting by name.

$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter()
->addOrderField('name');
3
  • Thank you but I should've added that I'm PHP illiterate so can you to tell me exactly where to put the code and, if relevant, what to replace?
    – bigbadbaz
    Nov 9, 2015 at 10:42
  • do you want to update in database or you just want collection? Nov 9, 2015 at 11:44
  • I want to update in the database. Marius' code permanently sorts categories and subcategories alphabetically, but only works on a single root category. I am looking for the same thing but for multiple root categories.
    – bigbadbaz
    Nov 9, 2015 at 19:54
0

After hours of searching and trying different scripts, I eventually found this one which works for me. It sorted all root and subcategories alphabetically in just a few seconds :)

Magento Sort Categories Alphabetically using SQL Query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.