Question

I am currently working with a client who has a large catalogue with hundreds of categories. We are in the process of restructuring their category tree and are finding that the Magento admin area is taking too long to do this.

Is there a way in which we can amend the category orders and parent IDs of categories programatically using some kind of script or maybe through the API?

I found the stack question below from another user when doing a similar task but this was posted a while ago and wondered if there was a better way of doing it.

https://stackoverflow.com/questions/3986344/magento-move-a-category-programmatically

Thanks for your help :-)

Was it helpful?

Solution

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);
$category->setPath('1/3/18/5')->save();

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 parent_id, position, level and children_count. All of these can be updated with the same technique as for path (i.e. setChildrenCount()).

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.

OTHER TIPS

What Jonathan suggested worked really well and enabled me to update the categories order very quickly. I also found that switching the index save modes from 'Update On Save' to 'Manual Update' also sped up the admin area and enabled me to save changes without having to wait hours. By the time I discovered this I had already written a script that used the method above to update them. Hope this helps :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top