[Magento] Overriding Core files

When building custom modules for Magento, one of the most commonneeds is to override Magento’s core files, most commonly Blocks,Models, Resources, and Controllers. And, by the way, when I say“override”, that is also synonymous with “rewrite” or “extend”.

I wanted to write this up for my own reference, but I hope thisends up helping you to. At the time of writing this, all of thesemethods have been tested on 1.4.0. This post assumes you to havefamiliarity with writing Magento modules. And remember, you onlyneed to include functions in your file that you are modifying.Leave all untouched functions out of your file.

Also, the reason I haven’t included much for examples of theactual block/model code is that 90% of getting a rewrite to workcorrectly is just getting your config.xml correct. It matters wayless of where you put your files in your module (though it’s goodto keep it organized and clean).


Overriding Core Blocks

One of the more simple and straight-forward things to overridein Magento. Let’s say that you want to override the followingclass: Mage_Catalog_Block_Product_View.

The first step is to copy the file into your own module’s Blockfolder. It can be anywhere you want within that folder, it reallydoesn’t matter. But, for organizational purposes, it’s always best,in my opinion, to keep a similar folder/file structure as Magentodoes. In this case, I would put this file inMy/Module/Block/Catalog/Product/View.php. Of course, you’ll need torename the class, and have it extendMage_Catalog_Block_Product_View.

Here is how the ‘blocks’ tag in your config.xml should look:

<blocks>
    <my_module>
        <class>My_Module_Block</class>
    </my_module>
    <catalog>
        <rewrite>
            <product_view>My_Module_Block_Catalog_Product_View</product_view>
        </rewrite>
    </catalog>
</blocks>

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’ tag tells Magento that we are going to override a block(since we are within the ‘blocks’ 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 we are using tooverride the core block.

As another example, if you wanted to overrideMage/Catalog/Block/Product/View/Type/Simple.php, the tag under‘rewrite’ would be ‘product_view_type_simple’.


Overriding Core Models

Overriding models (but not resource models, which are anythingdeclared in config.xml as ‘resourceModel’, which are typicallyfiles within a Mysql4 directory) is basically the same as blocks(above). So, I will give an example, but leave out much of theexplanation.

Lets say that I want to modify the model for the items on anorder invoice (Mage_Sales_Model_Order_Invoice_Item). I will copythat file to My/Module/Model/Sales/Order/Invoice/Item.php, renamethe class, and extend Mage_Sales_Model_Order_Invoice_Item.

The config.xml ‘models’ will look something like this:

<models>
    <my_module>
        <class>My_Module_Model</class>
    </my_module>
    <sales>
        <rewrite>
            <order_invoice_item>My_Module_Block_Sales_Order_Invoice_Item</order_invoice_item>
        </rewrite>
    </sales>
</models>

Overriding Core Resource Models

I found out the hard way once, and wasted a couple hours, thatresource models have a different way of overriding them. All of theconcepts are the same, with the exception of the syntax in yourconfig.xml file. A resource model is typically going to be modelsthat reside within a ‘Mysql4′ folder. The resource model folder istypically defined in the config.xml file using the tag‘resourceModel’.

I was putting together a dependent filter module, and I neededto override this class:Mage_Catalog_Model_Resource_Eav_Mysql4_Attribute. Just as the aboveexamples, I created this file:My/Module/Model/Catalog/Resource/Eav/Mysql4/Attribute.php, renamedthe class, and extendedMage_Catalog_Model_Resource_Eav_Mysql4_Attribute.

As I said above, the xml syntax changes for resource models.Instead of defining just the ‘catalog’ tag right before the‘rewrite’, you actually have to define all the way down to themysql4 folder. Here is an example for the above model:

<models>
    <my_module>
        <class>My_Module_Model</class>
    </my_module>
    <catalog_resource_eav_mysql4>
        <rewrite>
            <attribute>My_Module_Model_Catalog_Resource_Eav_Mysql4_Attribute</attribute>
        </rewrite>
    </catalog_resource_eav_mysql4>
</models>

Overriding Admin Core Controllers

I have seen numerous methods on how to do this, but the method Iwill describe seems to be the current ‘standard’.

Lets say that I need to override the adminhtml attributecontroller: Mage_Adminhtml_Catalog_Product_AttributeController.First thing is to create the controller in your module. I would putmine inMy/Module/controllers/Catalog/Product/AtttributeController.php. Animportant key to note here is that with controllers, Magento doesnot autoload them like it does with blocks and models. So, we’llneed to include the file of the controller that we want tooverride. Here is an example of how my controller would look:

<?php
include_once("Mage/Adminhtml/controllers/Catalog/Product/AttributeController.php");
class My_Module_Catalog_Product_AttributeController extends Mage_Adminhtml_Catalog_Product_AttributeController
{
...

The config.xml file is key now. Unlike models and blocks, youdon’t need to define exactly which/where controller you are needingto override. You just need to define whether it is an ‘admin’ or‘frontend’ controller, which module has the controller(s) you areoverriding, and which module you are overriding it with (your own,obviously).

Here is the example for the above controller:

<config>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <my_Module before="Mage_Adminhtml">My_Module</my_Module>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

Overriding Frontend Core Controllers

Lets override the Onepage Checkout controller:Mage_Checkout_OnepageController. First thing is to create thecontroller in your module. I would put mine inMy/Module/controllers/Checkout/OnepageController.php. An importantkey to note here is that with controllers, Magento does notautoload them like it does with blocks and models. So, we’ll needto include the file of the controller that we want to override.Here is an example of how my controller would look:

<?php
include_once('Mage/Checkout/controllers/OnepageController.php');
class My_Module_Checkout_OnepageController extends Mage_Checkout_OnepageController
{
...

The config.xml file is key now. Unlike models and blocks, youdon’t need to define exactly which/where controller you are needingto override. Unlink overriding an admin controller, here will willput our router info inside the ‘frontend’ tags.

Here is the example for the above controller:

<config>
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                         <My_Module before="Mage_Checkout">My_Module_Checkout</My_Module>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>

Please feel free to ask questions or provide feedback on thispost. If there are any errors or better ways to do any of this,please let me know.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值