Magento – Add Button to Sales Order View Page (Observer/Event)

magentomagento-1.5

I'm trying to add a custom printing button (like print invoice) on the Sales Order View page (Sales > Orders > Order #… view).

I've done this successfully with a but now have two modules that that same page. Therefore I'm trying to do the Observer/Event method and am running into trouble.

This is what I have for the Mass Action printing and it works great (previous page only (Sales > Orders).

$block = $observer->getEvent()->getBlock();

// Mass Action Printing option
if(get_class($block) =='Mage_Adminhtml_Block_Widget_Grid_Massaction'
     && $block->getRequest()->getControllerName() == 'sales_order')
     {
        $block->addItem('test_mass_print', array(
            'label' => 'Print Test',
            'url' => Mage::app()->getStore()->getUrl('orderforms/order/pdfTest'),
        ));
      }

Now when I try to add the button for the Product View page (under the same function):

// Order View Page button
        if(get_class($block) =='Mage_Adminhtml_Block_Sales_Order_View'
            && $block->getRequest()->getControllerName() == 'sales_order')
        {
            $this->_addButton('test_print', array(
                'label'     => Mage::helper('sales')->__('Test'),
                'onclick'   => Mage::app()->getStore()->getUrl('orderforms/order/print'),
                'class'     => 'go'
            ));
        }

I keep getting errors like this:

Fatal error: Call to undefined method Company_Test_Model_Observer::_addButton() in app/code/local/Company/Test/Model/Observer.php on line 24

I've tried:

  • $block->_addButton
  • $block->_addItem

but nothing seems to work. Why is this not working???

Best Answer

I solved it a little bit after I posted. The solution for me was the following:

// Order View Page button
        if(get_class($block) =='Mage_Adminhtml_Block_Sales_Order_View'
            && $block->getRequest()->getControllerName() == 'sales_order')
        {
            $block->addButton('test_print', array(
                'label'     => 'Test',
                'onclick'   => 'setLocation(\'' . $block->getUrl('html/sales_order/print') . '\')',
                'class'     => 'go'
            ));
        }
Related Topic