When building custom modules for Magento, one of the most common needs 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 this ends up helping you to. At the time of writing this, all of these methods have been tested on 1.4.0. This post assumes you to have familiarity with writing Magento modules. And remember, you only need 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 the actual block/model code is that 90% of getting a rewrite to work correctly is just getting your config.xml correct. It matters way less of where you put your files in your module (though it’s good to keep it organized and clean).
Overriding Core Blocks
One of the 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 purposes, 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. Of course, you’ll need to rename the class, and have it extend Mage_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 to override the core block.
As another example, if you wanted to override Mage/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 anything declared in config.xml as ‘resourceModel’, which are typically files within a Mysql4 directory) is basically the same as blocks (above). So, I will give an example, but leave out much of the explanation.
Lets 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:
<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, that 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.
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 the mysql4 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 I will describe seems to be the current ‘standard’.
Lets say that I need to override the adminhtml attribute controller: Mage_Adminhtml_Catalog_Product_AttributeController. First thing is to create the controller in your module. I would put mine in My/Module/controllers/Catalog/Product/AtttributeController.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 of the controller that we want to override. 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, you don’t need to define exactly which/where controller you are needing to override. You just need to define whether it is an ‘admin’ or ‘frontend’ controller, which module has the controller(s) you are overriding, 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 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 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, you don’t need to define exactly which/where controller you are needing to override. Unlink overriding an admin controller, here will will put 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 this post. If there are any errors or better ways to do any of this, please let me know.
相关推荐
Magento是一款强大的开源电子商务平台,以其高度可定制性和灵活性著称。在Magento的后台管理系统中,管理员可以进行商品的创建、编辑、管理等操作。然而,原生的Magento系统并未提供直接在后台预览商品前端展示效果...
### Magento新手指南知识点详解 #### 一、Magento简介与特性 **Magento**是一款功能强大的开源电子商务平台,由Varien公司(后被Adobe收购)于2008年首次发布。它以其灵活性、可扩展性和丰富的功能集而闻名,是...
通过SFTP或SSH导航到Magento [Magento]/app/code/ 。 上载带有扩展名的Dropday/OrderAutomation目录。 打开终端并导航到Magento根目录。 运行以下命令以启用Dropday扩展: php bin/magento module:enable Dropday_...
Magento:registered:2数据集成借助Datatric Connect扩展程序,可以轻松地将Magento:registered:2商店与Datatrics平台连接起来。安装在开始安装过程之前,我们建议您对webshop文件以及数据库进行备份。 有两种安装...
### Magento权威指南 #### 书籍概述 《Magento权威指南》是由Adam McCombs与Robert Banh共同编著的一本深入探讨Magento电商平台的技术手册。该书由Apress出版社于2009年出版发行,旨在为读者提供一个全面、系统的...
docker-magento:Mark Shust的Magento Docker配置
magento:megento电子商务网站
ansible-magento要求: -流浪汉-可以指示: 克隆此仓库无所事事在浏览器中导航到magento.dev。MySQL 数据库:magento 用户名:magento 密码:password123 这是基于的工作而进行的一些更改,以简化启动和运行过程...
Magento是一款开源的电子商务平台,由Adobe公司开发,用于构建高度可定制的在线商店。它基于PHP语言,利用了Zend框架,提供了丰富的功能和强大的性能,让商家能够灵活地管理产品、订单、客户以及营销活动。 在...
浏览器将请求发送到 Magento 所在的服务器,Magento 的 Controllers 捕捉该请求,并开始处理请求。 步骤 3:Controller 使用 Object Models Controller 使用 Object Models,这一步骤中,Magento 的 Model 开始...
VueFrontCMS Connect App for Magento 显示你的 :red_heart: -给我们一个 :star: 帮助我们将这个项目发展到最好! VueFront是CMS不可知的SPA和PWA前端,适用于您的老式Blog和电子商务网站。 Magento通过最佳的电子...
流浪者-magento:**已弃用**
$ cd /path/to/magento $ modgit init $ modgit add epicomtech_magento https://github.com/epicomtech/magento.git Antes deComeçar Epicom的API协议 通过电子邮件联系API,电子邮件地址: Conhecendo omó...
马恩托 magento 项目
Magento 2 是一款开源的电子商务平台,用于构建和管理在线商店。它由Adobe公司拥有,是Magento 1的升级版本,提供了许多改进的功能和性能优化。Magento 2 的设计目标是提供一个高度可定制、功能丰富的电商解决方案,...
Gimmie Magento 插件 为 Magento 添加 Gimmie 奖励,让用户在购买和推荐朋友时获得积分并获得真正的奖励。 要安装此插件,我们提供。 建造 克隆此插件所需的所有子模块(gimmie php 项目) $git submodule init &&...
以下是一些关于 Magento CE 2.4.0 的核心知识点: 1. **增强的安全特性**: - Magento 2.4.0 强化了安全防护,包括对 SQL 注入、XSS 攻击的防御,以及增强了密码策略和支付网关的加密。 - 集成了 PCI DSS...
"clean-magento: 测试技能magento 2" 的标题暗示我们将讨论的是与 Magento 2 的测试相关的技术,特别是如何对 Magento 2 系统进行清理和优化,以提高性能和稳定性。 在 Magento 2 开发过程中,测试是不可或缺的一...
Tryton Magento集成 Magento电子商务平台与Tryton ERP的集成。 文献资料 可获取该项目的详细文档。 该文档是自动生成的,欢迎您对文档进行改进或翻译。 在开始任何事情之前,请创建一个问题,以避免重复工作。 ...
Magento Python API 用于连接到Magento Web服务的Python库。 用法 import magento url = 'http://domain.com/' apiuser = 'user' apipass = 'password' # Create an instance of API client = magento . API ( url ...