There are some functions of Magento core which are written in Block, Model or Helper. When we use these functions due to our purposes we see that they are not suitable and as same as we wish. Therefore, we can utilize Override class feature of Magento to rewrite those functions according to our uses.
I – For block
For example: We need to rewrite app/core/Mage/Catalog/Block/Product/List.php
At first, we have to create a module including at least 3 files:
- app/code/local/Magestore/Newmodule/Block/Product/List
- app/code/local/Magestore/Newmodule/etc/config.xml
- app/etc/modules/Magestore_Newmodule.xml
• Step1: Declare the module (app/etc/modules/Magestore_Newmodule.xml) as in Configuration XML (in the last post) > Create and register a module.
• Step 2: Edit the file config.xml (app/code/local/Magestore/Newmodule/etc/config.xml)
<config>
<global>
<blocks>
<newmodule>
<class>Magestore_Newmodule_Block</class>
</newmodule>
<catalog>
<rewrite>
<product_list>Magestore_Newmodule_
Block_List</product_list>
</rewrite>
</catalog>
</blocks>
</global>
</config>
Through this step, when you call the block ‘catalog/product_list’ the system will return a block ‘newmodule/list’.
• Step 3: Override block (app/code/local/Magestore/Newmodule/Block/List.php)
<?php
Class Magestore_Newmodule_Block_List extends Mage_Catalog_Block_Product_List{
// function (need be written)
protected function_getProductCollection(){
//custom code
}
}
II – For Model
For instance: override app/code/core/Mage/Catalog/Model/Product.php)
By the file app/code/local/Magestore/Newmodule/Model/Catalog/Product.php
• Step 1: Create and register a new module (as same as the part above)
• Step 2: File config.xml(app/code/local/Magestore/Newmodule/config.xml)
<config>
<global>
<models>
<newmodule>
<class>Magestore_Newmodule_Helper</class>
</newmodule>
<catalog>
<rewrite>
<product>Magestore_Newmodule_Model_
Product</product>
</rewrite>
</catalog>
</models>
</global>
</config>
• Step3: Override model (app/code/local/Magestore/Newmodule/Model/Catalog/Product.php)
<?php
class Magestore_Newmodule_Model_Product extends Mage_Catalog_Model_Product
{
public function isSalable(){
}
}
III – For Resource Models
For example: override app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Attribute.php)
By the file: app/code/local/Magestore/Newmodule/Model/Catalog/Resource/Eav/Mysql4/Attribute.php
• Step 1: Create and register a new module (as same as the previous part)
• Step 2: File config.xml(app/code/local/Magestore/Newmodule/config.xml)
<config>
<global>
<models>
<newmodule>
<class>Magestore_Newmodule_Model</class>
</newmodule>
<catalog_resource_eav_mysql4>
<rewrite>
<attribute> Magestore_Newmodule_
Model_Catalog_Resource_Eav_Mysql4
_Attribute </attribute>
</rewrite>
< catalog_resource_eav_mysql4>
</models>
</global>
</config>
• Step 3: Override file (as same as the way to conduct with Model)
IV – For Helper
For example: override app/code/core/Mage/Catalog/Helper/Data.php)
By the file app/code/local/Magestore/Newmodule/Helper/Catalog/Data.php
• Step 1: Create and register a new module (as same as the part above).
• Step 2: File config.xml(app/code/local/Magestore/Newmodule/config.xml)
<config>
<global>
<helpers>
<newmodule>
<class>Magestore_Newmodule_Model</class>
</newmodule>
< catalog>
<rewrite>
<data> Magestore_Newmodule_Helper_
Catalog_Data </data>
</rewrite>
<catalog >
</helpers>
</global>
</config>
• Step 3: Override helper (app/code/local/Magestore/Newmodule/Helper/Catalog/Data.php)
<?php
class Magestore_Newmodule_Helper_Product_Catalog_Data extends Mage_Catalog_Helper_Data
{
public function getLastViewed(){
}
}
V – Notices
Rewriting Modules/Resource models/Helper/Block only has impact on calling objects through Mage classes such as: Mage::getModel(), Mage::getResourceModel(), Mage::helper(), Mage::getSingletonBlock(). With the generation of objects from different ways as new ($product = new Mage_Catalog_Model_Product(); ) or extends rewriting does not have influence. This is exactly the difference when we use direct object-calling from class name and the function which Magento provides.
VI – Questions
Question: Can overriding a resource model be conducted as same as overriding a normal model (because they are belong to the model area)?
Answer: You cannot configure the override of a resource model as same as a normal model. Because the override only has influence when the system calls the function Mage::getResourceModel(). This function reads the configuration rewrite from the bar of the resource model (for example: catalog_resource_eav_mysql4) but from the bar of the model.
Hope it is useful for you as for me!
6 Comments
Hi, can you explain me why don´t override a Resource Model ,who describe in the question VI. I don´t understand completly
Hi David,
It’s certain that you can override a Resource Model. The question of part VI means that “Can you override a Resource Model as same as override a Model?
(because they are together contained in Model category).”
And according to the answer, overriding a Resource Model must have the configuration different from that of overriding a Model.
Good day,
Michael
Hi Michael,
I’m a beginner in Magento, your post helped me a lot on how to override class, but my question whether we have create a new module for overriding class, model, resource model, helper individually or else we can override it all using a single module…
Hi,
You can using both single module for multiple modules to overwrite class, model, resource model… as you mentioned. If you have any other questions, you can leave comments here. I am glad to discuss with you. Nice day 🙂
Hi, for this IV – For Helper, app/code/local/Magestore/Newmodule/config.xml should have Magestore_Newmodule_Helper as a class name rather than
Magestore_Newmodule_Model
Hi Kajal,
Thank you very much for your comment.
I have just fixed my error 🙂