Reputation: 12588
I wish to print invoices using HTML, so have added a 'Print A4 Label' mass action, by copying app/code/core/Mage/Adminhtml/Block/Sales/Invoice/Grid.php
to app/code/local/Mage/Adminhtml/Block/Sales/Invoice/Grid.php
and added the following code:
$this->getMassactionBlock()->addItem('printa4label', array(
'label'=> Mage::helper('sales')->__('Print A4 Label'),
'url' => $this->getUrl('*/sales_invoice/printa4label'),
));
This has added a mass action option that points to index.php/admin/sales_invoice/printa4label/key/...
which currently gets a 404 error.
What file/directory do I need to create to handle this new mass action, and how do I handle the data that is sent?
app/code/local/GCT/Printlabel/etc/config.xml
<?xml version="1.0"?>
<config>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<GCT_Printlabel before="Mage_Adminhtml">GCT_Printlabel_Adminhtml</GCT_Printlabel>
</modules>
</args>
</adminhtml>
</routers>
</admin>
</config>
app/code/local/GCT/Printlabel/controllers/Adminhtml/Sales/Order/InvoiceController.php
<?php
require_once 'Mage/Adminhtml/controllers/Sales/Order/InvoiceController.php';
class GCT_Printlabel_Adminhtml_Sales_Order_InvoiceController extends Mage_Adminhtml_Sales_Order_InvoiceController
{
public function printlabelAction()
{
echo "Hello World";
exit;
}
}
app/etc/modules/GCT_Printlabel.xml
<?xml version="1.0"?>
<config>
<modules>
<GCT_Printlabel>
<active>true</active>
<codePool>local</codePool>
</GCT_Printlabel>
</modules>
</config>
app/code/local/Mage/Adminhtml/Block/Sales/Invoice/Grid.php
...
$this->getMassactionBlock()->addItem('printlabel', array(
'label'=> Mage::helper('sales')->__('Print Label'),
'url' => $this->getUrl('*/sales_invoice/printlabel'),
));
...
I am still getting the 404 error.
Upvotes: 0
Views: 904
Reputation: 941
You need to override Sales Invoice controller in your custom module and create an action named printlabel
Currently this controller does not have printlabelAction() so it is throwing you 404 error.
Basically it is looking for printlabelAction() in InvoiceController.php
The original controller file is situated here: app\code\core\Mage\Adminhtml\controllers\Sales\InvoiceController.php
It is a good practice to never change the core file. Hence you should overload the controller.
You can get the steps to overload a controller in custom module from here: http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/how_to_overload_a_controller
After the controller is overloaded in your custom module, open that file and add following to test:
public function printlabelAction() {
echo "Hello World";
exit;
}
UPDATE: Grid.php
$this->getMassactionBlock()->addItem('printlabel', array(
'label'=> Mage::helper('sales')->__('My Custom Print'),
'url' => $this->getUrl('*/sales_order_invoice/printlabel'),
));
config.xml file:
<admin>
<routers>
<adminhtml>
<args>
<modules>
<GCT_Printlabel before="Mage_Adminhtml">GCT_Printlabel_Adminhtml</GCT_Printlabel>
</modules>
</args>
</adminhtml>
</routers>
</admin>
Notice GCT_Printlabel_Adminhtml in above code. Make sure controller file is placed in proper folder structure. It should be in GCT/Printlabel/controllers/Adminhtml/Sales/Order/InvoiceController.php
Ensure you are using proper case everywhere to avoid issues related to case-sensitivity.
require_once 'Mage/Adminhtml/controllers/Sales/Order/InvoiceController.php';
class GCT_Printlabel_Adminhtml_Sales_Order_InvoiceController extends Mage_Adminhtml_Sales_Order_InvoiceController
{
public function printlabelAction()
{
echo "Hello World";
exit;
}
}
Clear all Magento cache and your browser cache, then run your mass action. It will display "Hello World" message.
Also try to avoid multiple capital letters in your module name in future. That sometimes causes issues. PrintA4Label should be Printa4label Hope this helps!!
Upvotes: 2