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.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: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.