Loading...

Magento is arguably the most powerful open-source e-commerce solution available. Every detail and setting is completely customizable which let's the store owner tweak everything to work as they wish. However, because the codebase is quite large, the smallest changes can seem like daunting tasks to the new Magento developer.

In this post, I'll share some useful tips for developing and debugging custom code on the Magento platform.

1. Use a proper debugger

Magento utilizes highly-specialized classes on top of a strong MVC foundation. Because of this, a typical HTTP request may result in hundreds of new objects being created and passed around different pieces of code and it can be difficult to trace. (Compare this to a typical PHP script, which simply executes from the top of a file to the end.) Having a good debugger like xdebug will allow you to do things like:

  • Trace through each line of the code
  • Halt execution at a specific line
  • Look at what value a particular variable contains at that point in time
  • See what code called the current method

It may be tempting to litter your code with "var_dump()"s to know what's going on, but that's both messy and unreliable. A solid debugger lets you run through code execution at your own pace, showing you exactly what's going on. This is especially important with Magento, as it has many moving parts. If you only take away one tip from this post, let it be this one! Having a proper debugger will easily cut your development time in half.

Here's a fantastic resource for debugging Magento with Xdebug.

Bonus tip: To halt execution when an error occurs, set a breakpoint inside the logException() method of app/Mage.php. You'll be able to see the full details of the error and where it occured. You'll then know where to place additional breakpoints in the code that is giving you trouble.

2. Never directly edit core files

Sometimes you can't simply change an option within Magento to get your desired functionality - changes to the core behavior may be needed. However, you should NEVER directly edit core files, as they will probably be overwritten during a future Magento update and be lost forever.

For files inside of app/code/core/[whatever], a simple trick is to copy the file into a similar path within app/code/local/[whatever]. When Magento needs to load a certain class, it will check inside the local folder first - if it finds code there, it will use that instead of the original. This works best for simple changes in existing logic. You should also consider doing this for template design changes.

If you need more-advanced functionality, such as observing events, your best bet is to create a custom module. There's more overhead required to getting your code up-and-running, but you end up having more control.

3. Track file changes with a VCS

Because there are so many files, it can be difficult to track what changes you've made. A good VCS like Git or Subversion will keep track of every change you've made. If something goes wrong, you can look back at the file history and see what you changed and on what day. If you work in a team, you can see each change made by your peers. Should you ever need to undo some changes that broke something, it will help you do that.

4. Use design debugging tools

Changing the look and feel of Magento can be difficult for designers new to the platform. A single page may be comprised of hundreds of small tempaltes, so knowing which ones to edit is key. Thankfully Magento comes with a "Template Hint" feature that shows you exactly which template file you'd need to edit. However, if you need help figuring out layout handles or how to show the slider block you created, a third-party module called "CommerceBug" will help you get that information. It has useful features like class lookups and also shows you all the XML options used to build the current page.

5. If you don't know, ask!

If you can't figure something out, and Google isn't helping, there are a ton of Magento developers out there willing to help. A favorite resource of mine is StackOverflow, where you can post your question/problem and the community will do their best to help answer it. Many developers also hang out in the Magento Forum and #magento on Freenode IRC, so feel free to ask your questions there. Sometimes there's no substitute to getting advice/help from others who have experienced the same issues before or are familiar with what you're trying to accomplish.

Summary

Just because Magento is a large platform doesn't mean developing for it has to be difficult. Armed with the right tools, you should be able to quickly debug problems and figure out what code you need to get the job done. This post was more of a general overview of certain topics; if you'd like to learn more, check out the links in each tip or leave a comment here.