2
votes

I'm trying to add custom block to the order view in Magento admin. I have followed this tutorial but it doesn't seem to work.

app/etc/modules/Webspeaks_MyModule.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Webspeaks_MyModule>
            <active>true</active>
            <codePool>local</codePool>
        </Webspeaks_MyModule>
    </modules>
</config>

app/code/local/Webspeaks/MyModule/etc/config.xml

<?xml version="1.0"?>
<config>
    <adminhtml>
        <layout>
            <updates>
                <webspeaks_mymodule>
                    <file>mymodule.xml</file>
                </webspeaks_mymodule>
            </updates>
        </layout>
        <events>
            <core_block_abstract_to_html_after>
                <observers>
                    <mymodule_custom_order_view_info>
                        <class>mymodule/observer</class>
                        <method>getSalesOrderViewInfo</method>
                    </mymodule_custom_order_view_info>
                </observers>
            </core_block_abstract_to_html_after>
        </events>
    </adminhtml>
</config>

app/code/local/Webspeaks/MyModule/Model/Observer.php

<?php
class Webspeaks_MyModule_Model_Observer {

    // This function is called on core_block_abstract_to_html_after event
    // We will append our block to the html
    public function getSalesOrderViewInfo(Varien_Event_Observer $observer) {
        $block = $observer->getBlock();
        // layout name should be same as used in app/design/adminhtml/default/default/layout/mymodule.xml
        if (($block->getNameInLayout() == 'order_info') && ($child = $block->getChild('mymodule.order.info.custom.block'))) {
            $transport = $observer->getTransport();
            if ($transport) {
                $html = $transport->getHtml();
                $html .= $child->toHtml();
                $transport->setHtml($html);
            }
        }
    }
}

app/design/adminhtml/default/default/layout/mymodule.xml

<?xml version="1.0"?>
<layout version="1.0">
    <!-- Adding the block in sales/order/view page -->
    <adminhtml_sales_order_view>
        <!-- You can change the reference to whatever you like. Look ate layout/sales.xml for more options -->
        <!-- This should be same in Model/Observer.php::getSalesOrderViewInfo() -->
        <reference name="order_info">
            <block type="mymodule/adminhtml_sales_order_view_info_block" name="mymodule.order.info.custom.block" template="mymodule/custom.phtml" before="order_history" />
        </reference>
    </adminhtml_sales_order_view>
</layout>

app/design/adminhtml/default/default/template/mymodule/custom.phtml

<?php $order = $this->getOrder() ?>
<div class="entry-edit box-left">
    <div class="entry-edit-head">
        <h4 class="icon-head"><?php echo $this->__('My Custom Block') ?></h4>
    </div>
    <fieldset>
        <div id="mymodule_custom_block">
            Special message
        </div>
    </fieldset>
</div>
<div class="clear"></div>

app\code\local\Webspeaks\MyModule\Block\Adminhtml\Sales\Order\View\Info\Block.php

<?php
class Webspeaks_MyModule_Block_Adminhtml_Sales_Order_View_Info_Block extends Mage_Core_Block_Template {

    protected $order;

    public function getOrder() {
        if (is_null($this->order)) {
            if (Mage::registry('current_order')) {
                $order = Mage::registry('current_order');
            }
            elseif (Mage::registry('order')) {
                $order = Mage::registry('order');
            }
            else {
                $order = new Varien_Object();
            }
            $this->order = $order;
        }
        return $this->order;
    }
}
1
did u get any solution for above issueDivya Sekar

1 Answers

1
votes

By default there are only a 2 options, to add it via plain xml. before the content, and after the content.

<adminhtml_sales_order_view>
       <reference name="content">
           <block type="core/template" name="test-1" template="test/template.phtml" after="sales_order_edit"/>
           <block type="core/template" name="test-2" template="test/template.phtml" before="sales_order_edit"/>
       </reference>
   </adminhtml_sales_order_view>    

but those templates will be shown in all tabs.

to insert into several parts, you have to add your template like:

   <adminhtml_sales_order_view>
       <reference name="order_tab_info">
           <block type="core/template" name="test-1" template="test/template.phtml" after="sales_order_edit"/>
           <block type="core/template" name="test-2" template="test/template.phtml" before="sales_order_edit"/>
       </reference>
   </adminhtml_sales_order_view>    
Watch out for the changed reference name. 

In the corresponding template `app\design\adminhtml\default\default\template\sales\order\view\tab\info.phtml` you can place your block everywhere you want

   <div id="order-messages">
       <?php echo $this->getChildHtml('order_messages') ?>
   </div>
   <?php echo $this->getChildHtml('order_info') ?>
   <?php echo $this->getChildHtml('test-1') ?>

The main problem is, that the sales_order_view is kinda hard coded, so it'll keep the structure.

hope that helps.

Also you can try this one as well.

you can follow the below link.http://sohelrana09.wordpress.com/2011/06/24/how-to-overwrite-sales-view-order-phtml-file-in-magento/