Magento 1.9 – Add New Button on Sales Order View

magento-1.9sales-order

I want to add new Button on Sales order view page. I created below mention code

observer file

<?php

class Ar_Orderstatus_Model_Observer
{

    public function addOrderstatusAction($observer)
    {
        $block = $observer->getEvent()->getBlock();
        if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) {
            $message = Mage::helper('sales')->__('Are you sure you want to Change Status?');

            $block->addButton('rto', 
                array( 'label' => Mage::helper('sales')->__('RTO'), 
                    'onclick' => "confirmSetLocation('{$message}', '{$block->getUrl('orderstatus/adminhtml_index/rto')}')", 'class' => 'go' ));

           $block->addButton('in_shipment', 
            array( 'label' => Mage::helper('sales')->__('In Shipement'), 
                'onclick' => "confirmSetLocation('{$message}', '{$block->getUrl('orderstatus/adminhtml_index/in_shipment')}')", 'class' => 'go' ));  
        }
    }
}

controller file

<?php

class Ar_Orderstatus_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action 
{
    protected function _initOrder()
    {
        $id = $this->getRequest()->getParam('order_id');
        $order = Mage::getModel('sales/order')->load($id);

        if (!$order->getId()) {
            $this->_getSession()->addError($this->__('This order no longer exists.'));
            $this->_redirect('*/*/');
            $this->setFlag('', self::FLAG_NO_DISPATCH, true);
            return false;
        }
        Mage::register('sales_order', $order);
        Mage::register('current_order', $order);
        return $order;
    }


public function testAction()
{
    if ($order = $this->_initOrder()) {
        try {
            $order->setState('rto', true)
                ->save();
            $this->_getSession()->addSuccess(
                $this->__('The order state has been changed.')
            );
        }
        catch (Mage_Core_Exception $e) {
            $this->_getSession()->addError($e->getMessage());
        }
        catch (Exception $e) {
            $this->_getSession()->addError($this->__('The order state has not been changed.'));
            Mage::logException($e);
        }
        $this->_redirect('adminhtml/sales_order/view', array('order_id' => $order->getId()));
    }

}






}

Best Answer

Follow bellow steps

Step : 1 app\etc\modules\AR_Orderstatus.xml

<?xml version="1.0"?>
<config>
    <modules>
        <AR_Orderstatus>
            <active>true</active>
            <codePool>local</codePool>
        </AR_Orderstatus>
    </modules>
</config>

Step : 2 app\code\local\AR\Orderstatus\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <AR_Orderstatus>
            <version>4.0.3</version>
        </AR_Orderstatus>
    </modules>
    <admin>
        <routers>
    <orderstatus>
        <use>admin</use>
        <args>
            <module>AR_Orderstatus</module>
            <frontName>orderstatus</frontName>
        </args>
    </orderstatus>
        </routers>
    </admin>
    <adminhtml>
        <events>
            <core_block_abstract_prepare_layout_before>
                <observers>
                    <orderstatus_core_block_abstract_prepare_layout_before>
                        <type>model</type>
                        <class>AR_Orderstatus_Model_Observer</class>
                        <method>addOrderstatusAction</method>
                    </orderstatus_core_block_abstract_prepare_layout_before>
                </observers>
            </core_block_abstract_prepare_layout_before>
        </events>
    </adminhtml>   
    <global>
        <models>
            <orderstatus>
                <class>AR_Orderstatus_Model</class>
            </orderstatus>
        </models>
    </global>
</config>

Step : 3 app\code\local\AR\Orderstatus\controllers\Adminhtml\IndexController.php

<?php

class Ar_Orderstatus_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action 
{
    protected function _initOrder()
    {
        $id = $this->getRequest()->getParam('order_id');
        $order = Mage::getModel('sales/order')->load($id);

        if (!$order->getId()) {
            $this->_getSession()->addError($this->__('This order no longer exists.'));
            $this->_redirect('*/*/');
            $this->setFlag('', self::FLAG_NO_DISPATCH, true);
            return false;
        }
        Mage::register('sales_order', $order);
        Mage::register('current_order', $order);
        return $order;
    }


    public function rtoAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $order->setState('rto', true)
                    ->save();
                $this->_getSession()->addSuccess(
                $this->__('The order state has been changed.')
                );
            }
            catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
            catch (Exception $e) {
                $this->_getSession()->addError($this->__('The order state has not been changed.'));
                Mage::logException($e);
            }
            $this->_redirect('adminhtml/sales_order/view', array('order_id' => $order->getId()));
        }

    }

    public function shipmentAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $order->setState('shipment', true)
                    ->save();
                $this->_getSession()->addSuccess(
                    $this->__('The order state has been changed.')
                );
            }
            catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
            catch (Exception $e) {
                $this->_getSession()->addError($this->__('The order state has not been changed.'));
                Mage::logException($e);
            }
            $this->_redirect('adminhtml/sales_order/view', array('order_id' => $order->getId()));
        }

    }
}

Step : 4 app\code\local\AR\Orderstatus\Model\Observer.php

<?php

class Ar_Orderstatus_Model_Observer
{

    public function addOrderstatusAction($observer)
    {
        $block = $observer->getEvent()->getBlock();
        if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) {
            $message = Mage::helper('sales')->__('Are you sure you want to Change Status?');

            $block->addButton('rto', 
                array( 'label' => Mage::helper('sales')->__('RTO'), 
                    'onclick' => "confirmSetLocation('{$message}', '{$block->getUrl('orderstatus/adminhtml_index/rto')}')", 'class' => 'go' ));

           $block->addButton('in_shipment', 
            array( 'label' => Mage::helper('sales')->__('In Shipement'), 
                'onclick' => "confirmSetLocation('{$message}', '{$block->getUrl('orderstatus/adminhtml_index/shipment')}')", 'class' => 'go' ));  
        }
    }
}
Related Topic