Magento admin panel custom button in order view page.
Magento admin panel custom button in order view page.

Now in this article I am going to show you how simply you can set Magento admin panel custom button in order view page.Generally it can be done by overriding sales_order_view block into your custom module but I can only prefer ot do in observer because it won’t affect any ones module or never depend upon anyone’s module .For example if some might be already overrides that code in your site then you can’t do it again.Either you can place your code into their module or extend the block who has override the core sales_order_view block.This problem will occur if you will try to override but if you will do through observer then do independently. So.So lets see how we can implement Magento admin panel custom button in order view page.For that you need to have little concept of event observer in magento. So.So then after you can understand easily all the below explanations with code.Here i am not explaing everything form beginning of module I mean I am not explaining how to create a module because module creation is not our concern.Our concern is how to add custom button.Simply for managing through observer we need to two files one config.xml another is Observer.php where our desire method will place.

Config.xml:

app/code/[CodePool]/[NameSpace]/[ModuleName]/config.xml
This is the simple module declation file where we can declare every functionality or rout of work.So lets add our new work or functionality in this file.So it would be at event section as we are going to set event and to capture that.
In my case I have a module called JRB_Custombutton.Then My config.xml file would be at app/code/local/JRB/Custombutton/config.xml

<config>
    ..........................
    ..........................
    
    <global>
        ......................
        ......................
        <models>
            <custombutton>
                <class>JRB_Custombutton_Model</class>
                <resourcemodel>custombutton_mysql4</resourcemodel>
            </custombutton>
	</models>
        
        ............................
        ............................
        ............................
        
        <events>
            <adminhtml_widget_container_html_before><!-- identifier of the event we want to catch -->
                <observers>
                    <custombutton><!-- identifier of the event handler -->
                        <class>custombutton/observer</class><!-- observers class alias -->
                        <type>model</type><!-- class method call type; valid are model, object and singleton -->
                        <method>adminhtmlWidgetContainerHtmlBefore</method><!-- observer's method to be called -->
                        <args></args> <!-- additional arguments passed to observer -->
                    </custombutton>
                </observers>
            </adminhtml_widget_container_html_before>    
        </events>
    </global>
</config>

Observer.php

This is the file which has the observer class and we can declare our method which we have defined in config.xml file.
app/code/[CodePool]/[NameSpace]/[ModuleName]/Observer.php
e.g: app/code/local/JRB/Custombutton/Observer.php

<?php
class JRB_Custombutton_Model_Observer
{
    public function adminhtmlWidgetContainerHtmlBefore(Varien_Event_Observer $observer)
    {
	$block = $observer->getBlock();
        
	if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) {
	    $message = Mage::helper('core')->__('Are you sure you want to do this?');
	    $block->addButton('custom_button', array(
		'label'     => Mage::helper('core')->__('Custom Button'),
		'onclick'   => "confirmSetLocation('{$message}', '{$block->getUrl('*/custombutton/mycontroller')}')",
		'class'     => 'go'
	    ));
	}
    }
}
?>

The above code set the custom button and points to a controller url by onclick evetn on button. Because We don’t only the button If the button do some work then there meaning to set the button.So the url set there points to module controller file where you can do your desire work lie export order etc. or you can do any custom work over there by passing order id or any argument as parameter through the url.

Still if you feel your overriding concept is better then you can follwo below code for Magento admin panel custom button in order view page.Lets see how that ce be.
Same concept as above we need to have two file one is config.xml file another is our overrides file that is View.php.

Config.xml:
app/code/[CodePool]/[NameSpace]/[ModuleName]/config.xml
e.g: app/code/local/JRB/Custombutton/config.xml

<config>
    ..........................
    ..........................
    
    <global>
        ......................
        ......................
        
        <blocks>
            <adminhtml>
               <rewrite>
                   <sales_order_view>JRB_Custombutton_Block_Adminhtml_Sales_Order_View</sales_order_view>
               </rewrite>
           </adminhtml>
       </blocks>
    </global>
</config>

In above code we have override by rewrite to “sales_order_view” and set method in our module then after we extend core file to our module file.lets see below.

View.php:

app/code/[CodePool]/[NameSpace]/[ModuleName]/Block/Adminhtml/Sales/Order/View.php
e.g: app/code/local/JRB/Custombutton/Block/Adminhtml/Sales/Order/View.php

<?php
class JRB_Custombutton_Block_Adminhtml_Sales_Order_View extends Mage_Adminhtml_Block_Sales_Order_View {
    public function  __construct() {

        parent::__construct();

        $this->addButton('custom_button', array(
		'label'     => Mage::helper('core')->__('Custom Button'),
		'onclick'   => "confirmSetLocation('{$message}', '{$this->getUrl('*/custombutton/mycontroller')}')",
		'class'     => 'go'
	));
    }
}
?>

The above code has the same concept but only different is, it just overrides Mage_Adminhtml_Block_Sales_Order_View class.

Magento admin panel custom button in order view page
Tagged on:                         

Leave a Reply

Your email address will not be published. Required fields are marked *