0

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()));
    }

}






}
2

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' ));  
        }
    }
}
  • I followed your step but no change – Magento 2 Dec 9 '15 at 5:55
  • What are issues? – Abdul Dec 9 '15 at 5:57
  • Button not display on order view page – Magento 2 Dec 9 '15 at 5:59
  • Updated ans pls check now – Abdul Dec 9 '15 at 6:25
  • Button is working but state didn't chage? – Magento 2 Dec 9 '15 at 6:39
3

Both answers are not wrong but would trigger on each layout or widget load. The best solution is to add an Event for

controller_action_layout_render_before_adminhtml_sales_order_view

config.xml of your module:

<adminhtml>
    <events>
        <controller_action_layout_render_before_adminhtml_sales_order_view>
            <observers>
                <module_button>
                    <class>namespace_module/observer</class>
                    <method>addButtonOfYourChoice</method>
                </module_button>
            </observers>
        </controller_action_layout_render_before_adminhtml_sales_order_view>
    </events>
</adminhtml>

in your Namespace_Module_Model_Observer:

/**
 * @param Varien_Event_Observer $observer
 * @return $this
 */
public function addButtonOfYourChoice(Varien_Event_Observer $observer)
{
    $block = Mage::app()->getLayout()->getBlock('sales_order_edit');
    if (!$block) {
        return $this;
    }
    $order = Mage::registry('current_order');
    $url   = Mage::helper("adminhtml")->getUrl(
        "adminhtml/yourmoduleurl/youraction",
        array('order_id' => $order->getId())
    );
    $block->addButton(
        'button_id',
        array(
            'label'   => Mage::helper('namespace_module')->__('Button Label'),
            'onclick' => 'setLocation(\'' . $url . '\')',
            'class'   => 'go'
        )
    );
    return $this;
}

now in your action your can trigger, change, manipulate whatever you want

Regards

2

app/etc/modules/Solsint_Cancelorder.xml

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

app/code/local/Solsint/Cancelorder/etc/config.xml

 <?xml version="1.0"?>
<config>
    <modules>
        <Solsint_Cancelorder>
            <version>0.1.0</version>
        </Solsint_Cancelorder>
    </modules>

    <global>
        <models>
          <cancelorder>
            <class>Solsint_Cancelorder_Model</class>
          </cancelorder>
        </models>
     <events>
      <sales_order_save_commit_after>
            <observers>
                <sales_order_save_commit_after>
                    <class>cancelorder/Observer</class>
                    <method>sendreviewEmailOnComplete</method>
                </sales_order_save_commit_after>
            </observers>
        </sales_order_save_commit_after>
    </events>
    </global>
    <adminhtml>  
        <events>
          <adminhtml_widget_container_html_before>
                <observers>
                    <adminhtml_widget_container_html_before>
                        <class>Solsint_Cancelorder_Model_Observer</class>
                        <type>singleton</type>
                        <method>addbuttoninorder</method>
                    </adminhtml_widget_container_html_before>
                </observers>
            </adminhtml_widget_container_html_before>
        </events>
     </adminhtml> 
</config>

in your observer class app/code/local/Solsint/Cancelorder/Model/Observer.php

<?php 
class Solsint_Cancelorder_Model_Observer{


public function sendreviewEmailOnComplete($observer)
{ 
    $order = $observer->getOrder();Mage::log('working');
    if($order->getState() == Mage_Sales_Model_Order::STATE_CANCELED)
    { Mage::log('working');
     $comment='';
     $orderload=Mage::getModel('sales/order')->load($order->getEntityId());
     $orderload->sendOrderUpdateEmail(true, $comment);
    }
}
  public function addbuttoninorder($event)
{ Mage::log("adadadasd");

    $block = $event->getBlock();


    if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) {
        $message = Mage::helper('sales')->__('Are you sure you want to Change Status?');
        $block->addButton('change_status', array(
            'label'     => Mage::helper('sales')->__('Change status'),

            'onclick'   => "confirmSetLocation('{$message}', '{$block->getUrl('modulename/path/')}')",

            'class'     => 'go'
        ));

    }

}
}

?>
  • can you please tell me the path name also – Magento 2 Dec 9 '15 at 5:17
  • follow my last answer and xml after the </global> tag and observer is also there just add function – Qaisar Satti Dec 9 '15 at 5:19
  • can i add config.xml & Observer.php – Magento 2 Dec 9 '15 at 5:27
  • no just ad xml in config.xml that are already don't need to create new. also for observer just add method in that – Qaisar Satti Dec 9 '15 at 5:28
  • Sorry really i didn't get exact path – Magento 2 Dec 9 '15 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.