2

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

0

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.

  • Thanks for your reply Jonathan. Point taken with regards to updating them via admin to ensure that all fields get updated. We are going to rearrange the categories on a local development area with the idea of pushing all the changes to the live server. How would you suggest we carry across the changes to the categories? As doing them all individually via the admin area would take a very long time. – Ryan Copeland Nov 20 '14 at 9:33
  • If the dev area is an identical clone of production then you could go along the lines of update dev, export the catalog_category_entity table, import into production. This isn't something I have ever tried however so I would test this by importing the table into another test environment first. Also there aren't any foreign key checks on that particular table but there will be on other tables referring to this one so disable key checks first otherwise you could end up with a lot of really wrong category data. – Jonathan Hussey Nov 20 '14 at 10:08
  • Thanks for your help Jonathan. Will give this a go and let you know how I get on. :-) – Ryan Copeland Nov 21 '14 at 10:40
0

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

  • Glad it worked for you, I'm guessing you don't have rep to have made this a comment? – Jonathan Hussey Nov 26 '14 at 11:53
  • Not yet Jonathan but very close :-) – Ryan Copeland Nov 27 '14 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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