When building custom modules for Magento, one of the most common needs is to override Magento’s core files, commonly Blocks, Models, Resources, and Controllers.
Override Core Blocks
One of more simple and straight-forward things to override in Magento. Let’s say that you want to override the following class: Mage_Catalog_Block_Product_View.
The first step is to copy the file into your own module’s Block folder. It can be anywhere you want within that folder, it really doesn’t matter. But, for organizational purpose, it’s always best, in my opinion, to keep a similar folder/file structure as Magento does. In this case, I would put this file in My/Module/Block/Catalog/Product/View.php, and have it extends Mage_Catalog_Block_Product_View.
Here is how the ‘blocks’ tag in your config.xml should look:
My_Module_Block My_Module_Block_Catalog_Product_View
As you can see, we’ve got the rewrite xml inside of the ‘catalog’ tag. This refers to app/code/core/Mage/Catalog/. Then the ‘rewrite’ tab tells Magento that we arg going to override a block (since we are within the ‘blog’ tag) under Mage/Catalog/. The ‘product_view’ tag points to Mage/Catalog/Block/Product/View.php, and within that tag is the name of the class that wwe are going to override the core block.
As another example, if you wanted to override Mage/Catalog/Block/Product/View/Simple.php, the tag under ‘rewrite’ would be ‘product_view_type_simple’.
Override Core Models
Overriding Models is basically the same as Blocks.
Let’s say that I want to modify the model for the items on an order invoice (Mage_Sales_Model_Order_Invoice_Item). I will copy that file to My/Module/Model/Sales/Order/Invoice/Item.php, rename the class, and extend Mage_Sales_Model_Order_Invoice_Item.
The config.xml ‘models’ will look something like this:
My_Module_Model My_Module_Model_Sales_Order_Invoice_Item
Again, ‘sales’ refers to Mage_Sales Model and ‘order_invoice_item’ refers to Mage_Sales_Model_Order_Invoice_Item.
Overriding Core Resource Models
Resource models have a different way of overriding them. All of the concepts are the same, with the exception of the syntax in your config.xml file. A resource model is typically going to be models that reside within a “Mysql4” folder. The resource model folder is typically defined in the config.xml file using the tag ‘resourceModel’.
I was putting together a dependent filter module, and I needed to override this class: Mage_Catalog_Model_Resource_Eav_Mysql4_Attribute. Just as the above examples, I created this file: My/Module/Model/Catalog/Resource/Eav/Mysql4/Attribute.php, renamed the class, and extended Mage_Catalog_Model_Resource_Eav_Mysql4_Attribute.
My_Module_Model My_Module_Model_Catalog_Resource_Eav_Mysql4_Attribute
The xml syntax changes for resource models. Instead of defining just the ‘catalog’ tag right before ‘rewrite’, you actually have to define all the way down to the mysql4 folder.
Overriding Frontend Core Controllers
Lets override the Onepage Checkout controller: Mage_Checkout_OnepageController. First thing is to create the controller in your module. I would put mine in My/Module/controllers/Checkout/OnepageController.php. An important key to note here is that with controllers, Magento does not autoload them like it does with blocks and models. So, we’ll need to include the file to the controller that we to override. Here is the example for my Onepage CheckoutController:
The config.xml file is key now. Unlike models and blocks, you don’t need to define exactly which/where controller you are needing to override.
Here is the config.xml for our example:
My_Module_Checkout