Magento – Magento: add button on order view page with custom module

adminhtmlmagento-1.9moduleorderssales-order

I want to add a button on the Order View page of the Admin.

I created a custom module in app\code\community\CPT\TCS and added subfolders Block, controllers, etc, Helper, Model, sql

This is my CPT_TCS.xml file in app\etc\modules\

CPT_TCS.xml:

<?xml version="1.0"?>
<config>
     <modules>
        <CPT_TCS>
            <active>true</active>
            <codePool>community</codePool>
            <depends>
                <Mage_Adminhtml />
            </depends> 
        </CPT_TCS>
     </modules>
</config>

This is my custom module's etc\config.xml:

<?xml version="1.0" ?>
<config>
  <modules>
    <CPT_TCS>
      <version>
        1.0.0
      </version>
    </CPT_TCS>
  </modules>

  <blocks>
    <cpttcs>
      <class>CPT_TCS_Block</class>
    </cpttcs>
    <adminhtml>
      <rewrite>
        <sales_order_view>CPT_TCS_Block_Adminhtml_Sales_Order_View</sales_order_view>
      </rewrite>
    </adminhtml>
  </blocks>
</config>

Now I've added view.php in TCS\Block\Adminhtml\Sales\Order\

<?php
class CPT_TCS_Block_Sales_Order_View extends Mage_Adminhtml_Block_Adminhtml_Sales_Order_View
{

    public function __construct() {

        //parent constructor
        parent::__construct();

        //create URL to our custom action
        $url = Mage::helper("adminhtml")->getUrl(
            "CPT_TCS/order/doSomething",
            array('order_id'=>$this->getOrder()->getId())
        );

        //add the button
        $this->_addButton('cygtest_resubmit', array(
                'label'     => Mage::helper('sales')->__('Send TCS'),
                'onclick'   => 'setLocation(\'' . $url . '\')',
                'class'     => 'go'
        ));

    }

}

The "Send TCS" button is not showing on order view page (this "Send TCS" button should ultimately send data order data on web service).

What can be wrong here?

Best Answer

I think your problem is that the <blocks> tag from your config.xml is not in the right place.
It should be a child of the node <global>

So make your config.xml look like this:

<?xml version="1.0" ?>
<config>
  <modules>
    <CPT_TCS>
      <version>
        1.0.0
      </version>
    </CPT_TCS>
  </modules>
  <global>
  <blocks>
    <cpttcs>
      <class>CPT_TCS_Block</class>
    </cpttcs>
    <adminhtml>
      <rewrite>
        <sales_order_view>CPT_TCS_Block_Adminhtml_Sales_Order_View</sales_order_view>
      </rewrite>
    </adminhtml>
  </blocks>
  </global>
</config>

Alternative
You don't need to rewrite the order view block to add a button. you can do it cleaner with an observer.
Add this in the config.xml of your module under the config root note.

<adminhtml>
    <events>
        <controller_action_layout_render_before_adminhtml_sales_order_view>
            <observers>
                <ctp_tcs>
                    <class>ctptcs/adminhtml_observer</class>
                    <method>addTcsButton</method>
                </ctp_tcs>
            </observers>
        </controller_action_layout_render_before_adminhtml_sales_order_view>
    </events>
</adminhtml>

Then declare your models in the same config.xml.
Under the <global> tag add this:

<models>
    <cpttcs>
        <class>CPT_TCS_Model</class>
    </cpttcs>
</models>

Then create the observer class CTP/TCS/Model/Adminhtml/Observer.php

<?php 
class CTP_TCS_Model_Adminhtml_Observer 
{
    public function addTcsButton($observer) {
        $block = Mage::app()->getLayout()->getBlock('sales_order_edit');
        if (!$block){
            return $this;
        }
        $order = Mage::registry('current_order');
        $url = Mage::helper("adminhtml")->getUrl(
            "CPT_TCS/order/doSomething",
            array('order_id'=>$order->getId())
        );
        $block->addButton('cygtest_resubmit', array(
                'label'     => Mage::helper('sales')->__('Send TCS'),
                'onclick'   => 'setLocation(\'' . $url . '\')',
                'class'     => 'go'
        ));
        return $this;
    }
}