Magento admin How to add button in sales invoice view page?

by Mandal   Last Updated January 12, 2018 08:09 AM

enter image description here

app\etc\modules\button_module.xml

<config>
    <modules>
        <button_module>
            <active>true</active>
            <codePool>core</codePool>

        </button_module>
    </modules>
</config>

app/code/local/Button/Module/etc/config.xml

<adminhtml>
    <events>
        <adminhtml_widget_container_html_before>
            <observers>
                <button_module>
                    <class>button_module/observer</class>
                    <method>adminhtmlWidgetContainerHtmlBefore</method>
                </button_module>
            </observers>
        </adminhtml_widget_container_html_before>
    </events>
</adminhtml>

app/code/local/Button/Module/controllers/Adminhtml/Sales/InvoiceController.php

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Adminhtml') . DS . 'Sales' . DS . 'InvoiceController.php';
class Your_Module_Adminhtml_Sales_InvoiceController extends Mage_Adminhtml_Sales_InvoiceController
{  
    public function viewinvoicespageAction() {
        $invoiceIds = $this->getRequest()->getParam('invoice_ids');
    }
}
?>

app/code/local/Button/Module/Model/Observer.php

<?php
class Button_Module_Model_Observer
{
    public function adminhtmlWidgetContainerHtmlBefore($event)
    {  
        $paramsarray = Mage::app()->getRequest()->getParams('invoice_id');
        $invoicesid = $paramsarray["invoice_id"];
        $block = $event->getBlock();
        if ($block instanceof Mage_Adminhtml_Block_Sales_Order_Invoice_View) {
            $block->addButton('do_something_crazy', array(
                'label'     => Mage::helper('module')->__('Button'),
                'onclick'   => "setLocation('{$block->getUrl('*/sales_invoice/viewinvoicespage/invoice_ids/'.$invoicesid)}')",
                'class'     => 'go'
            ));          
        }
    }
}
?>


Answers 2


You should update your module filename to Button_Module.xml with capital letters. Then update the content of the file like below.

<?xml version="1.0"?>
<config>
    <modules>
        <Button_Module> <!-- Capital Letters -->
            <active>true</active>
            <codePool>local</codePool> <!-- Correct codepool from 'core' to 'local' -->
        </Button_Module>
    </modules>
</config>
Jaimin Sutariya
Jaimin Sutariya
January 12, 2018 08:21 AM

I believe the most correct approach would be by calling \Mage_Adminhtml_Block_Widget_Container::addButton method on your invoice...

So you would need to Create a layout XML file for your module and add the button in your invoice view page, as follows:

<layout>
    <adminhtml_sales_order_invoice_view>
        <reference name="sales_invoice_view">
            <action method="addButton">
                <id>my_btn</id>
                <data>
                    <label>My button</label>
                    <onclick><![CDATA[
                    alert('hello world');
                    ]]></onclick>
                </data>
            </action>
        </reference>
    </adminhtml_sales_order_invoice_view>
</layout>
Ricardo Martins
Ricardo Martins
April 29, 2018 05:25 AM

Related Questions