Extension Sneak Peek: Associated Categories

One of the joys of working with Magento is the ability to customize and modify it to handle almost anything. I was recently tasked with building an extension that offered a way to clone/share one categories products with another, ie Associated Categories.

This extension is ideal for a situation when you have an external product feed being mapped into your Magento install that includes a category structure that does not easily relate to your stores. The aim of this is to allow you to keep both your category structure and the external one by simply assigning/mapping the categories of the external source to your own as makes sense to you, all from the Magento back-end.

The main motive behind the design I came up with was to make these associations easy for the user to implement and understand. To accomplish this I added a new tab to the “Manage Categories” area on the Magento back-end, that shows which categories are associated to the current one and provides a familiar “Category Tree” style layout to select categories that you would like to share the current categories products with.

(click photos for higher resolution)

Behind the scenes of this extension the two main components are an event observer, listening when a category is saved, and a custom indexer. These ensure that the categories are updated both in real time when changes are made to a category, and on a schedule to catch any new changes or products that may have been added from an external source. While this project had many challenges there were 2 main considerations that made it interesting.

First was the fact that we are mapping/cloning one category to another, it was imperative to keep in mind and work around the possibility of creating an infinite loop where two categories end up mapped to each other. The other issue was creating the custom category tree, as this (like many things we do in Magento) didn’t show up with any results when trying to find examples/tutorials where it had been done before. The only solution was to yet again spend many hours working my way back through the core Magento code (as I know all Magento developers have done before me) to figure out how to manipulate it into doing what was needed. In the end, I’m very happy with the outcome and glad to learn more about the ever expanding possibilities when working with Magento.