Magento – Adding Button to Adminhtml Order View Not Working

adminhtmlblocksmagento-communitymagento-enterprisesales-order

I know this is a question that has been asked a few times before, however, I have spent most the day trying various solutions (not least from StackExchange) and examples which I've tried to reference below.

The issue I have is that I wish to add a custom button to the Order view within the adminhtml section of Magento (enterprise, 1.14.0.1), that's it! Just add the button! I've created the module triple checked the namespaces, spellings and paths, even used other modules that do achieve this as a base, all to no avail!

So I've rolled a simple example module that should JUST add a button that reloads the order view. It's super simple, 3 files! The problem is the constructor just never seems to be called (I've been adding Mage::log's in there to see).

Can anyone in the know please take a look and help point out if/where I've gone wrong, please?

For the benefit of the tape, the Company name for the module is "Cygnus" and the module name is "Test".

Let's start with the config.xml (/app/code/community/cygnus/test/etc/config.xml)

<?xml version="1.0"?>
<config>
    <modules>
        <Cygnus_Test>
            <version>0.0.1</version>
        </Cygnus_Test>
    </modules>
    <blocks>
        <cygnustest>
            <class>Cygnus_Test_Block</class>
        </cygnustest>
        <adminhtml>
            <rewrite>
                <sales_order_view>Cygnus_Test_Block_Adminhtml_Sales_Order_View</sales_order_view>
            </rewrite>
        </adminhtml>
    </blocks>
</config>

Now, here's the other XML config for completeness (/app/etc/modules/Cygnus_Test.xml)

<?xml version="1.0"?>
<config>
    <modules>
        <Cygnus_Test>
            <active>true</active>
            <codePool>community</codePool>
        </Cygnus_Test>
    </modules>
</config>

And finally the block extension (/app/code/community/cygnus/test/Block/Adminhtml/Sales/Order/View.php)

<?php
class Cygnus_Test_Block_Adminhtml_Sales_Order_View extends Mage_Adminhtml_Block_Sales_Order_View 
{

    public function  __construct() {

        Mage::log("I am the constructor, YAY!!!!",null,'cyg.log');

        //This was to test if the parent constructor makes a difference
        //parent::__construct();

        //create current URL
        $url = $this->getOrder()->getId();
        $url = $this->getUrl('*/sales_order/view', array('order_id'=>$current_order_id));

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

        //don't forget the parent!
        parent::__construct();

    }

}

So, nothing happens. No log from the constructor. I have tested the _addButton code in another module and this works, likewise, as you can see I've moved the parent constructor around but this makes no difference as the issue appears to be that Magento does not find or recognize my View.php file. It all points to a config.xml issue but I (and my senior developer) can not see the issue.

PS> I have, of course, been developing and testing locally with manual and admin based cache clearance (all caches are off anyway), and done many reloads/login & out of admin resets.

Anyone have anything to suggest? please feel free to copy/paste the code down to test. I stopped short of trying the observer method (in ref 4) instead of focusing on why my rewrite is not functioning.

References (sorry but not enougth rep yet to post 2+ links 🙁 ) :

  1. https://stackoverflow.com/questions/6642599/how-to-add-new-button-to-order-view-in-magento-admin-panel
  2. https://stackoverflow.com/questions/24835742/magento-admin-custom-button-on-view-order
  3. https://magento.stackexchange.com/questions/49709/button-click-not-working-on-sales-order-view-custom-tab-section
  4. Magento Admin Custom Button on View Order
  5. http://www.silvatechsolutions.com/tech-news/custom-magento-admin-order-buttons-fix-on-upgrade/
  6. http://ravichandranjothi.blogspot.co.uk/2013/06/magento-how-to-add-button-to-order-view.html
  7. https://magentomods.wordpress.com/2012/10/04/how-to-add-a-custom-button-to-an-admin-page-magento-ee-1-12-0-2/
  8. http://alanstorm.com/custom_magento_system_configuration

Thanks so much for advise and help in advance!

Danny 'Blatant'
cygnus

Best Answer

I think you can make use of an observer method instead of using a rewrite. The following code should do the trick:

public function coreBlockAbstractPrepareLayoutBefore(Varien_Event_Observer $observer)
{
    $block = $observer->getBlock();
    if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) {
            $block->addButton('your_button', array(
                'label'     => 'your_label',
                'onclick'   => 'whatever_should_happen';
        }
    }
}

However if you want to use your module the config.xml is missing a global node. I think that's why your rewrite is not working.

Edit: Your Config.xml sould like:

<?xml version="1.0"?>
<config>
   <modules>
       <Cygnus_Test>
           <version>0.0.1</version>
       </Cygnus_Test>
   </modules>
   <global>
       <blocks>
        ...
Related Topic