The location of each category in the tree is set by it's path which you can see defined in the database table
catalog_category_entity. The path is just a
/ separated list of category ids defining the location of the category. This will always start with
1 being the highest level category which is not actually displayed in the category view. After that you will see the id of all categories that lead to the category you are interested in. For instance in the demo the path for the mens shoes category under apparel is
1/3/18/5/16. In order to set a new location for this category you can simply change this path, so setting it to
1/3/18/5 would change the category location to be under apparel rather than shoes.
So to change the location of this particular category in the tree you can simply do:
$category = Mage::getModel('catalog/category')->load(16);
And the category will have moved to a child of the apparel rather than shoes category. However, an important note. There are several other steps you need to take to edit the category so that other logic relating to determining the location of the category must be changed too (for instance the call to
getLevel() on the category would now be wrong. So along with the path you also will need to update
children_count. All of these can be updated with the same technique as for
If you are needing to update a lot of categories with anything more than a pretty simple tree configuration then determining what each of these values needs to be for every category is not going to be an easy task. My advice would be to stick to the admin way of doing things to be sure all the information ends up correct, and if you want to try this method all same then try in a safe environment first, testing afterwards and proceed with caution.