Magento is more than e-commerce platform – it’s a great framewrok based on Zend Framework. One of its best features is modularity (far better then in ZF) and possibility of overriding core classes.You can rewrite blocks, models and helpers with the use of configuration file. This way you get new functionality by enabling your custom module leaving original code intact. Suppose you want to change behavior of Mage_Catalog_Model_Product class. You can create your own class (say MageDev_Catalog_Model_Product) extending core class. Module should be located in the local pool – here goes directory structure:
app/code/local/MageDev/Catalog – contains module files
app/code/local/MageDev/Catalog/etc – here go configuration files
app/code/local/MageDev/Catalog/Model – custom class extending Mage_Catalog_Model_Product will be stored in this directory
Rewrites are defined in app/code/local/MageDev/Catalog/etc/config.xml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?xml version="1.0"?> <config> <modules> <MageDev_Catalog> <version>0.0.1</version> </MageDev_Catalog> </modules> <global> <models> <catalog> <rewrite> <product>MageDev_Catalog_Model_Product</product> </rewrite> </catalog> </models> </global> </config> |
Now you just need to activate the module in app/etc/modules/MageDev_Catalog.xml file:
1 2 3 4 5 6 7 8 9 10 11 12 | <?xml version="1.0"?> <config> <modules> <MageDev_Catalog> <active>true</active> <codePool>local</codePool> <depends> <Mage_Catalog /> </depends> </MageDev_Catalog> </modules> </config> |
From now on all calls to
Mage::getModel('catalog/product'); |
will return MageDev_Catalog_Model_Product instance (if this class is defined in app/code/local/MageDev/Catalog/Model/Product.php file).
Suppose I’d like to extend MageDev_Catalog_Model_Product_Class and don’t want to change existing module. To do this I have to create another module. Let’s have a look at the new module config file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?xml version="1.0"?> <config> <modules> <MageDev_NewCatalog> <version>0.0.1</version> </MageDev_NewCatalog> </modules> <global> <models> <catalog> <rewrite> <product>MageDev_NewCatalog_Model_Product</product> </rewrite> </catalog> </models> </global> </config> |
At this stage Magento during initialization of modules has to choose which class to use to rewrite Mage_Catalog_Model_Product. In order to solve this issue Magento sorts rewrites using dependencies. You can define dependency in config file located at app/etc/modules directory. For the two classes presented above these files might look like:
app/etc/modules/MageDev_Catalog.xml:
1 2 3 4 5 6 7 8 9 10 11 12 | <?xml version="1.0"?> <config> <modules> <MageDev_Catalog> <active>true</active> <codePool>local</codePool> <depends> <Mage_Catalog /> </depends> </MageDev_Catalog> </modules> </config> |
app/etc/modules/MageDev_NewCatalog.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version="1.0"?> <config> <modules> <MageDev_NewCatalog> <active>true</active> <codePool>local</codePool> <depends> <Mage_Core /> <Mage_Catalog /> <MageDev_Catalog /> </depends> </MageDev_NewCatalog> </modules> </config> |
Now Magento will firstly load MageDev_Catalog and later MageDev_NewCatalog using rewrites from the latest module.
It’s worth to note that when new module is installed you may need to change dependency in your config file to address rewrites done by the new module. This is another good reason to install new modules in your test environment.



The best information i have found exactly here. Keep going Thank you
Where did you take from such kind of information? Can you give me the source?
Hi Cris,
Thanks for interest. As you may know there’s no official Magento guide for developers so you need to make your own exploration of the source code. Although at first glance it might seem quite complex, after some time you’ll discover how well designed the Magento code is.
Really helpful.
Thanks a lot
It works too for the blocks you just need to replace the tag ‘models’ by ‘blocks’ (and close it of course) in the config.xml
Nice tutorial.
But how do you do this for onepage checkout block?
Hello George,
Here’s a part of config file that rewrites onepage checkout:
If you want to rewrite onepage checkout steps you should rewrite blocks in app/code/core/Mage/Checkout/Block/Onepage/ directory. Hope that helps.
hello Admin,
i need to rewrite Magento’s core Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection as Mycompany_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
For that i created a new module ‘Mycompany’ under app/code/local/. Now i placed this collection.php file under Mycompany_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
under app/code/local/Mycompany/Catalog/Model/Resource/Eav/Mysql4/Product/.
I enabled this module in app/etc/modules/Mycompany_All.xml
In app/code/local/Mycompany/etc/config.xml i added following xml code:
Eyemagine_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
but still this core model collection is not being rewrite.
What i am doing wrong, please guide me
spent big time on this issues but no luck … Need some guidelines.
please check this for the solution of my above narrated problem: http://www.magentocommerce.com/boards/viewthread/35787/
@Admin: can you please explain why this rewrite way is different then you explained ???
(I don’t blame your one is wrong, that is also right)
Hello Randy,
I can’t see xml code in your previous message but I guess it was overriding catalog in a way similar to:
while it works only when you override catalog_resource_eav_mysql4:
To find out the model id (used with Mage::getModel(model_id)) you need to use in rewrite declaration go to config file of module in which original class is located (in case of Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection it is app/code/core/Mage/Catalog/etc/config.xml). Then try to locate the longest prefix of the class inside
<models />tag.
In xml above you can locate
inside
<catalog_resource_eav_model />id so you should use this id to rewrite classes which names starts with Mage_Catalog_Model_Resource_Eav_Mysql4.
Hope this helps.
hmmm … i was unaware of this way ..
Thanks for explaining, your description sounds logical
hi admin, teach me how to see the results of model, block, or helper override plz?
Very nice info! Exactly what I was looking for
Keep up the good work.
Happy coding
magento rewrite rule is complex: block rewrite, models rewrite, router rewrite …
I want to override admin Catelog Product Controller for mass Action ..How can I? Can you provide example?