Magento Add Custom Block to Sales Order Info (Backend)

Magento Add Custom Block to Sales Order Info (Backend)

In this wiki i will show you how to add custom block to the sales order info in magento backend using observer,
you always see that  i am use observer , using observer is the best thing in magento because the code will work even there are multi observer , Overriding make some problems sometimes when there are multi extension use the same class

Company Name: Zeo

Module Name : OrderCustomBlock

1. we need to make the xml of extension file Zeo_OrderCustomBlock.xml

File Name: Zeo_OrderCustomBlock.xml

Path:  app\etc\modules\Zeo_OrderCustomBlock.xml

Code:

<?xml version="1.0"?>
<config>
  <modules>
    <Zeo_OrderCustomBlock>
      <active>true</active>
      <codePool>local</codePool>
      <version>1.0.0</version>
    </Zeo_OrderCustomBlock>
  </modules>
</config>

2. Let us cretae the configuration file

File Name: config.xml

File Path: app\code\local\Zeo\OrderCustomBlock\etc\config.xml

Code:

<?xml version="1.0"?>
<config>
  <modules>
    <Zeo_OrderCustomBlock>
      <version>1.0.0</version>
    </Zeo_OrderCustomBlock>
  </modules>
  <global>
    <helpers>
      <ordercustomblock>
        <class>Zeo_OrderCustomBlock_Helper</class>
      </ordercustomblock>
    </helpers>
    <blocks>
      <ordercustomblock>
        <class>Zeo_OrderCustomBlock_Block</class>
      </ordercustomblock>
    </blocks>
     <models>
      <ordercustomblock>
        <class>Zeo_OrderCustomBlock_Model</class>
      </ordercustomblock>
    </models>
  </global>
  <admin>
    <routers>
      <ordercustomblock>
        <use>admin</use>
        <args>
          <module>Zeo_OrderCustomBlock</module>
        </args>
      </ordercustomblock>
    </routers>
  </admin>
  <adminhtml>
   <events>
      <core_block_abstract_to_html_after>
        <observers>
          <zeo_ocb_core_block_abstract_to_html_after>
            <class>Zeo_OrderCustomBlock_Model_Adminhtml_Observer</class>
            <method>addOrderCustomBlock</method>
          </zeo_ocb_core_block_abstract_to_html_after>
        </observers>
      </core_block_abstract_to_html_after>
    </events>
    <layout>
      <updates>
        <ordercustomblock>
          <file>zeo/ordercustomblock.xml</file>
        </ordercustomblock>
      </updates>
    </layout>
  </adminhtml>
</config> 

3. Create helper File

File Name: Data.php

File Path: app\code\local\Zeo\OrderCustomBlock\Helper\Data.php

Code:

<?php
class Zeo_OrderCustomBlock_Helper_Data extends Mage_Core_Helper_Abstract
{
}
 

4. Create the Observer

File Name : Observer.php

File Path: app\code\local\Zeo\OrderCustomBlock\Model\Adminhtml\Observer.php

Code:

<?php
class Zeo_OrderCustomBlock_Model_Adminhtml_Observer{
    public function addOrderCustomBlock(Varien_Event_Observer $observer){
        $block = $observer->getBlock();
        if(($block->getNameInLayout() == 'order_info') && ($child = $block->getChild('zeo.order.info.customblock'))){
            $transport = $observer->getTransport();
            if($transport){
                $html = $transport->getHtml();
                $html .= $child->toHtml();
                $transport->setHtml($html);
            }
        }
        
    }
}

Design Section

1. XML Layput file

File Name: ordercustomblock.xml

File Path: app\design\adminhtml\default\default\layout\ zeo\ordercustomblock.xml

Code:

<?xml version="1.0"?>
<layout version="0.1.0">
    <adminhtml_sales_order_view>
        <reference name="order_info">
            <block type="adminhtml/sales_order_view_info" name="zeo.order.info.customblock" template="zeo/ordercustomblock/sales/order/view/custom_block.phtml" before="order_history" />
        </reference>
    </adminhtml_sales_order_view>
</layout>

 

2. phtml File

File Name:custom_block.phtml

File Path: app\design\adminhtml\default\default\template\ zeo\ordercustomblock\sales\order\view\custom_block.phtml

Code:

<?php $order = $this->getOrder(); ?>
<div class="entry-edit">
    <div class="entry-edit-head">
        <h4 class="icon-head"><?php echo $this->__('Custom Block') ?></h4>
    </div>
    <fieldset>
        This is my custom Block Order Increment=<?php echo $order->getIncrementId()?>
    </fieldset>
</div>
<div class="clear"></div>

That it :)

in case there is any question , Please contact us

Click here to see the code on Github

In case you see this information useful for you , please like our page to see all new posts