3

i need to add a custom button on admin sales order grid page like this:
enter image description here
I'm trying to create a module for this using this code in my config.xml file:

   <adminhtml>
        <events>
            <adminhtml_widget_container_html_before>
                <observers>
                    <crononline>
                        <class>crononline/observer</class>
                        <method>addNewButton</method>
                        <type>singleton</type>
                    </crononline>
                </observers>
            </adminhtml_widget_container_html_before>
        </events>
    </adminhtml>

and then add button by observer in this way

public function addNewButton($observer)
{
    $container = $observer->getBlock();
    if(null !== $container && $container->getType() == 'adminhtml/sales_order') {
        $data = array(
            'label'     => 'My button',
            'class'     => 'some-class',
            'onclick'   => 'setLocation(\' '  . Mage::getUrl('*/*', array('param' => 'value')) . '\')',
        );
        $container->addButton('my_button_identifier', $data);
    }

    return $this;
}

but with no luck.
Any help?

2 Answers 2

5

I've got it works.
Here the complete files:

<config>
  <modules>
    <Company_Module>
      <version>0.1.0</version>
    </Company_Module>
  </modules>
  <admin>
    <routers>
      <Company_Module>
        <use>admin</use>
        <args>
          <module>Company_Module</module>
          <frontName>companymodule</frontName>
        </args>
      </Company_Module>
    </routers>
  </admin>
  <adminhtml>
    <events>
      <adminhtml_widget_container_html_before>
        <observers>
          <unique-identifier>
            <type>singleton</type>
            <class>Company_Module_Model_Observer</class>
            <method>addNewButton</method>
          </unique-identifier>
        </observers>
      </adminhtml_widget_container_html_before>
    </events>
  </adminhtml>
</config>

and the observer

<?php
class Company_Module_Model_Observer
{
    public function addNewButton($observer)
    {   
        $container = $observer->getBlock();
        if(null !== $container && $container->getType() == 'adminhtml/sales_order') {
            $data = array(
                'label'     => 'Scarica Anagrafica',
                'class'     => '',
                'onclick'   => 'setLocation(\''  . Mage::helper('adminhtml')->getUrl('companymodule/adminhtml_controller/action') . '\')',
            );
            $container->addButton('unique-identifier', $data);
        }

        return $this;
    }
}

Hope will help someone

1

Override the class Mage_Adminhtml_Block_Sales_Order or else copy the file app\code\core\Mage\Adminhtml\Block\Sales\Order.php to app\code\local\Mage\Adminhtml\Block\Sales\Order.php

and change the _construct function like this

public function __construct()
{
    $this->_controller = 'sales_order';
    $this->_headerText = Mage::helper('sales')->__('Orders');
    $this->_addButtonLabel = Mage::helper('sales')->__('Create New Order');
    $this->_addButton('custom_button', array(
        'label'     => Mage::helper('sales')->__('Custom Button'),
        'onclick'   => "location.href='".$this->getUrl('*/*/custombutton')."'",
        'class'     => '',
    ));
    parent::__construct();
    if (!Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/create')) {
        $this->_removeButton('add');
    }
}

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.