Categories – How to Display Alphabetically Using Magmi

categorymagmi

We've been importing a lot of data into Magento CE using Magmi. It's creating our category tree for us.

However, we'd like to display our category tree in alphabetical order. Is there a way to sort the entire tree (20 top level categories, 100's of sub-categories in the tree) into alphabetical order?

Edit: for clarity, the sorting needs to be done programmatically in some way

Best Answer

If you have only one language so there is only one way to sort categories alphabetically I recommend changing their position so you will let Magento do its magic.
If you have more than one language, or you need the categories sorted alphabetically only on frontend ignore this answer.

Here is a small script that sets the category position.
$parentId - it the category id for which we sort the children
$conn - is a database connection because there is no need to involve the full internal API for changing only one field. we can run queries directly on the DB. I know that this is a big 'NO-NO' but in this case it works.

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);
    }
}

Now that we have the function we just need to run it.
For this create a file. Let's call it categories.php and place it in the root of your Magento instance. The file should contain this:

<?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);

You can add the function defined above in the same file below these lines

Now you just need to call in the browser mysite.com/categories.php.
Clear the cache and reindex the category flat data and you are done.
Just to make sure, backup your database before trying it.