Enough to be Dangerous Powerful Information Technology

2May/110

Unobtrusive Magento Modifications

As much as we all love Magento, there are times when we need it to do more for us.  Fortunately for us, the good folks that bring us Magento had the foresight to open source the code, so we aren't held captive by the vendor hoping they'll implement a feature we crave.  However, having access to the source code is a double-edged sword.  Sure, we could just go in the code and change whatever we want.  But, once we go down that path, upgrades become a nightmare and we risk compromising the quality of the overall core.  Therefore, we want any customizations we make to be unobtrusive.

There are 3 basic ways to properly modify Magento code.  Properly modifying simply means that we will not be changing any of the core files.

Override - create the exact same folder structure as the class you are trying to override in app/code/local/Mage.  This file will be called instead of the one from the core folder. 

Rewrite - redirect a specific core class to another class.

Observer - create a custom listener to handle events generated by the core.

Let’s take these one by one and describe the pros and cons.

Override

This will completely replace the core file with your own version of the file.  This is useful for those parts of Magento that are incredibly hard to get to.  The downfall to this is that when the core file is upgraded, you get none of the upgrade functionality and often times will find that you have to manually figure out how to get your override to work with the new core functionality.

I tell our developers only to do this as a last resort and it has to be approved before it’s placed into production.

Rewrite

This works somewhat like overriding.  You can create your module with your own class and then setup a rewrite telling Magento that you would rather the core file be found in this other class.  The key here is to only include functions that you want to overload and have your class extend the core file.  This will allow you to just replace one function from the core code leaving it a bit more manageable when time for an upgrade.  This is my preferred method if using an observer is not possible.

Observers

If your situation allows it, using observers is the best way to go.  This is the least obtrusive way to modify code.  The core code will fire off an event of some kind.  Your custom code will listen for that event and do what’s needed.  Read more on using observers...


 

Filed under: Magento Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.