I think everyone who is interested in ecommerce development get familiar with Magento earlier or later. It’s a very popular platform, where a lot of modern programming technologies are used. Since Magento is one of the most powerful and flexible shopping cart engines, it requires a deep level of expertise to develop extensions or even do some basic changes. So we’ve decided to share our tips for Magento Developers in this article. [Continue to Part 2]
You can get tons of solutions from the Internet on how to make one or another modifications for Magento functionality, but it’s worth noting that not all of them are smart, guarantee results and follow best practices.
Every developer eventually gets to the experience level that allows him to follow good developing practice. I want to share my own findings with you. So lets start.
Never use app/code/local/Mage to override core files.
Instead of this always try to create your own extension to change/add/modify core logic. It saves you from errors upon further store upgrades or third party extension’s collisions. If you ever use
app/code/local/Mage, then only use it for temporary changes that you will remove afterwards.
Try to avoid overriding wherever it’s possible.
Use event-listeners, helpers, or extend (not override) the core classes to form your own. It also saves your extension from conflicts with third party addons. For example, you always can use event core_block_abstract_to_html_after to inject button or some other element into html where there is no ability to inject it properly using xml layouts. There’s no reason to override .phtml files or block’s logic. For example, if you want to add/remove logic for checkout.onepage.billing block you need to create your own extension and specify your block class within xml layout.
<block type="your_extension/checkout_onepage_billing" name="checkout.onepage.billing" as="billing" template="checkout/onepage/billing.phtml"/>
Where
class Your_Extension_Block_Checkout_Onepage_Billing extends Mage_Checkout_Block_Onepage_Billing
Do not remove generic blocks from *.phtml files or xml layouts.
For example, you don’t need a generic block with name ‘product_additional_data‘ (product view page) and you think you are able to delete it from code. But you should not do this. Magento third party extensions usually use generic blocks to inject their own blocks there. If you have deleted it, you may spend a lot of time to find out why some extension doesn’t work.
Use Magento generic CSS classes whenever possible.
There are thousands of CSS classes, that are defined in Magento default installation and if you are going to make a new store template, you should use those classnames in your new template. It’s a good practice, because there is a lot of third party extensions that use those classnames for design integration.
Always document your own code.
As a good programmer, you should always use PHPDoc in your projects. If you were faced with problem and trying to find out the solution, it’s always easier to explore documented code. Also you should think about other programmers who may be forced to work with your code :)
Always explore core abstract classes during your work with Magento.
It helps you to make your code more effectively. For example I met many developers who write their own methods for checking a status of their extensions (enabled/disabled) but only few of them use method isModuleEnabled() from Mage_Core_Helper_Abstract
Use cache for your own blocks.
Magento allows to use cache system separately for custom blocks. Using cache for the blocks may greatly improve your extensions performance. To enable cache for the block you need to use the following part of code in your block constructor:
class Your_Extension_Block_Blah extends Mage_Core_Block_Template { protected function _construct() { parent::_construct(); $this->addData(array( 'cache_lifetime' => 43200, 'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG), )); } }
Use Magento built in log system for logging the behaviour of your extension.
It always helps to find issues faster if any. You are able to create/use your own log using the code:
Mage::log('There was a bug', null, 'log_filename.log');
Don’t forget to check if logging is enabled in Admin -> System -> Configuration -> Developer -> Debug .
Use Magento built in profiler to check the performance issues.
Magento allows to display loading time for separate parts at the bottom of every page for development purposes. First, enable the profiler via System -> Configuration -> Developer -> Debug -> Profiler (yes). Second, comment out the following line in index.php
#Varien_Profiler::enable();
Then you should see profiler on the store pages.
Use XML layouts to integrate features or extend functionality wherever it’s possible.
Magento allows to make different actions within layout XML files. As you know, it is possible to integrate your block somewhere in the design, but you can call different methods more efficiently using XML. Usually programmers forget about this feature. For example, you need to extend admin order’s grid with additional column. You can always extend Mage_Adminhtml_Block_Sales_Order_Grid but it’s not the best solution because it may be overwritten within some other extension. Instead of this, you can call method AddColumn, AddColumnAfter or some other directly from xml layout. Take a look at the example below:
<adminhtml_sales_order_grid> <reference name="sales_order.grid"> <action method="addColumnAfter"> <columnId>street</columnId> <arguments module="yourmodule" translate="header"> <header>Street</header> <type>text</type> <index>sales_flat_order_address.street</index> </arguments> <after>entity_id</after> </action> </reference> </adminhtml_sales_order_grid> </layout>
Use IDE (Integrated Development Environment).
There are many useful IDE to make Magento developing process faster, easier and more pleasant. You can try the most popular of them and choose which one is the best for you. There are many advantages of using IDE. You can inspect parent and abstract classes faster since most of IDE’s supports classes inheritance inspection. Popular IDE’s have the debugger in their toolset. It also may help you to save your time. Version control support, syntax checking, deployment management, smart snippets, refactoring, projects management and much more you can find in the modern IDE’s. As you can see, one application replaces many tools you have used before. All you need, is to find a convenient IDE for you and customise every detail.
To be continued… Part 2.
P.S. It would be great to see your own findings of Magento development in the comments.