4

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?

1
  • Check if your rewrite is actually working - link Jun 3, 2015 at 10:39

1 Answer 1

10

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;
    }
}
3
  • can u write complete config.xml code or a complete procedure how to add button on order view page because i write your code but it nothing happen no error show, no button show nothing else
    – Umar Ali
    Jun 3, 2015 at 10:57
  • make sure that your module declaration file exists in app/etc/modules.
    – Marius
    Jun 3, 2015 at 12:35
  • yes my module exist in app/etc/modules and show in system/configuration/advanced
    – Umar Ali
    Jun 4, 2015 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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