Override/Rewrite Magento core blocks and controllers
After spending many hours in rewriting block and controller of Magento core module, I finally came up with a solution.
Here I am going to rewrite block: Mage/Adminhtml/Block/Sales/Shipment/Grid.php
and controller: Mage/Adminhtml/controllers/Sales/ShipmentController.php
First you will need to make a xml file for your new module at app/etc/modules directory
CompanyName_Adminhtml.xml
<?xml version="1.0"?> | |
<config> | |
<modules> | |
<CompanyName_Adminhtml> | |
<active>true</active> | |
<codePool>local</codePool> | |
</CompanyName_Adminhtml> | |
</modules> | |
</config> |
Then, make folders in your app/code/local directory as follows:
– CompanyName
-> Block
—> Sales
—-> Shipment
——> Grid.php
-> controllers
—> Sales
—-> ShipmentController.php
-> etc
—> config.xml
In etc/config.xml, your code should look like below:
<?xml version="1.0"?> | |
<config> | |
<modules> | |
<CompanyName_Adminhtml> | |
<version>0.1.0</version> | |
</CompanyName_Adminhtml> | |
</modules> | |
<global> | |
<blocks> | |
<adminhtml> | |
<rewrite> | |
<sales_shipment_grid>CompanyName_Adminhtml_Block_Sales_Shipment_Grid</sales_shipment_grid> | |
</rewrite> | |
</adminhtml> | |
</blocks> | |
<routers> | |
<adminhtml> | |
<rewrite> | |
<sales_shipment> | |
<from><![CDATA[#^/admin/sales_shipment/$#]]></from> | |
<to>/admin/sales_shipment/</to> | |
</sales_shipment> | |
</rewrite> | |
</adminhtml> | |
</routers> | |
</global> | |
<admin> | |
<routers> | |
<adminhtml> | |
<args> | |
<modules> | |
<CompanyName_Adminhtml before="Mage_Adminhtml">CompanyName_Adminhtml</CompanyName_Adminhtml> | |
</modules> | |
</args> | |
</adminhtml> | |
</routers> | |
</admin> | |
</config> |
In ShipmentController.php, you should start like this:
require_once("Mage/Adminhtml/controllers/Sales/ShipmentController.php"); | |
class CompanyName_Adminhtml_Sales_ShipmentController extends Mage_Adminhtml_Sales_ShipmentController | |
{ | |
//controller methods goes here.. | |
} |
require_once is necessary as magento is not going to load controllers as it does for blocks and models
In block Grid.php, start the file like below:
class CompanyName_Adminhtml_Block_Sales_Shipment_Grid extends Mage_Adminhtml_Block_Widget_Grid | |
{ | |
// block methods goes here.. | |
} |
That’s it! Now you should get your local Grid.php and ShipmentController.php loading instead of core’s.
25 Comments
Leave a comment
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)
Cool resource! Thanks for creating it. Keep going that way.
Love the blog
Hey Kalpesh,
Buddy, I want to make ordercontroller.php over ride of the admin panel.
path is app/code/core/Mage/Adminhtml/controllers/sales/OrderController.php….
Blocks are working fine but controller is not working … please post the solution as soon as possible.
Thanks,
Amit Maurya
Hi Amit, use this code in your custom module’s config.xml file:
Hey kalpesh,
Dude this controller is not loading…. can you fix this..
Thanks for posting buddy
Amit Maurya
You just helped me with a extension i am working on thanks bro.
great work..
I want to override Mage/Adminhtml/controllers/Sales/OrderController.php. My module is app/code/local/MW/Adminhtml.
In my config.xml I have put the following code.
controller is as follows.
But still it loads the core controller class. What am I doing wrong here? Any help would be appreciated.
EDITED by kalpesh.. for formatting the code
Your code seems fine. The next thing I would do is clear the cache, Mage::log(__FILE__) on the top of MW/Adminhtml/controllers/Sales/OrderController.php file to check if the file is getting called or not AND check the app/etc/modules/MW_Adminhtml.xml file to make sure the module is active. Also please check the path of your module, it should be app/code/local/MW/Adminhtml/controllers/Sales/OrderController.php and app/code/local/MW/Adminhtml/etc/config.xml
I checked all the ways you mentioned. Then I removed all the things and created a new module and continued the same developing strategy. Now it works fine. I’m not sure what was missing in my code. Thank you so much Kalpesh. You are doing a great work. keep it up
Glad to hear it worked for you!
I want to override Mage/Adminhtml/controllers/Sales/OrderController.php. My module is app/code/local/Globalecom/Ordercomment
This is my config.xml
app/code/local/Globalecom/Ordercomment/etc/config.xml
Globalecom_OrderComment
this is my controller file
app/code/local/Globalecom/OrderComment/controllers/Sales/OrderController.php
require_once ‘Mage/Adminhtml/controllers/Sales/OrderController.php’;
class Globalecom_OrderComment_Sales_OrderController extends Mage_Adminhtml_Sales_OrderController
{
public function addCommentAction(){
……
// get the login info of current user
$_user = Mage::getSingleton(‘admin/session’);
$user[’email’] = $_user->getUser()->getEmail();
$user[‘firstname’] = $_user->getUser()->getFirstname();
$user[‘lastname’] = $_user->getUser()->getLastname();
$order->addStatusHistoryComment($data[‘comment’] . ” Added by {$user[‘firstname’]}”, $data[‘status’])
->setIsVisibleOnFront($visible)
->setIsCustomerNotified($notify);
}
}
Kindly check it out
I would also check if Mage_Adminhtml is not overriden by other module as well. PHP, and hence Magento, doesn’t support Multiple Inheritance. So you may need to extend the module which is already overriding the Mage_Adminhtml module in order to get this work.
Hi,
It means if we want want to override an already overridden class, we need to extend the overriden class instead of core file.
Eg:
class A
{
// This is the core class
}
class B extends A
{
}
If class C need to override class A
it should be
class C extends B
{
}
right?
yes you are right.
please put your XML code inside { code } tag. { code }the outer tags should be without spaces{ / code } .. E.g.
Sry Kalpesh,
I tried to put the code here. But it’s not showing. I’ll put a mail to you.
Thank you
Hi. I have edited your comment to format the code properly.
Hi Kalpesh,
Thank you for your response. I can’t figure out what am I doing wrong here? Can you point it out?
Fatal error: Call to a member function updateBackButtonUrl() on a non-object in /var/www/html/shipping/app/code/core/Mage/Adminhtml/controllers/Sales/Order/ShipmentController.php on line 135
I want to override the sales_order_shipment_view.
Please let me know how can I do this . It is really urgent.
please help me.
To override sales_order_shipment_view block, I think you should instead do like this in your local module’s config.xml file (replace Namespace_Module_Block_Adminhtml_Shipment_View with your local module file path):
hello kalpesh please help me, It is not working on my end , please check it and let me know how to solve this issue.
Its very urgent!!!!
Waiting for your reply…
config.xml
0.1.0
CompanyName_Adminhtml_Block_Adminhtml_Sales_Order_Shipment_View
/admin/sales_shipment/
CompanyName_Adminhtml
app/code/local/CompanyName/Adminhtml/Block/Adminhtml/Sales/Order/Shipment/View.php
<?php
class CompanyName_Adminhtml_Block_Adminhtml_Sales_Order_Shipment_View extends Mage_Adminhtml_Block_Widget_Form_Container
{
// die("CompanyName_Adminhtml_Block_Sales_Shipment_Grid");
}
app/code/local/CompanyName/Adminhtml/controllers/Sales/ShipmentController.php
<?php
require_once("Mage/Adminhtml/controllers/Sales/ShipmentController.php");
class CompanyName_Adminhtml_Sales_ShipmentController extends Mage_Adminhtml_Sales_ShipmentController
{
//controller methods goes here..
}
But still getting following error
Fatal error: Call to a member function updateBackButtonUrl() on a non-object in /app/code/core/Mage/Adminhtml/controllers/Sales/Order/ShipmentController.php on line 135
config.xml
0.1.0
CompanyName_Adminhtml_Block_Adminhtml_Sales_Order_Shipment_View
/admin/sales_shipment/
CompanyName_Adminhtml
”
0.1.0
<!–
CompanyName_Adminhtml_Block_Sales_Shipment_Grid
–>
CompanyName_Adminhtml_Block_Adminhtml_Sales_Order_Shipment_View
/admin/sales_shipment/
CompanyName_Adminhtml
“