Magento – Add custom block to admin order view

adminadminhtmlblockslayout

I am trying to add a custom block to the admin order view page and am doing this via an observer using the event core_block_abstract_to_html_before. I am using the giftmessage block, clone it and add it as a child in my own block

Observer code:

public function insertAdminBlock($observer)
{
    $_block = $observer->getBlock();
    $_type = $_block->getType();

    if ($_type == 'adminhtml/template')
    {
        if ($_block->getChild('order_giftmessage'))
        {
            $_child = clone $_block;
            $_child->setType('adminhtml/sales_order_view_info');
            $_block->setChild('giftmessage', $_child);
            $_block->setTemplate('mymodules/adminview.phtml');

        }
    }
}

the template looks like this:

<div class="entry-edit">
    <div class="entry-edit-head">
        <h4><?php echo Mage::helper('sales')->__('Custom Value') ?></h4>
    </div>
    <fieldset>
    <tr>

             <td class="label"><label><strong><?php echo Mage::helper('sales')->__('Custom Value:') ?></strong></label></td>
             <td class="value"><?php var_dump($this->getOrder()->getValue())?></td>
        </tr>
    </fieldset>
</div>

<?php echo $this->getChildHtml('giftmessage') ?>

The block gets inserted between the Payment/Shipping Method and Items block.

However the function $this->getOrder does not work in my block as the result is empty. Any ideas what I am missing?

Best Answer

  1. The order_giftmessage block pulls the order from the registry. You are free to do the same.
  2. To prepend markup to an existing block on-the-fly, use the core_block_abstract_to_html_after event.

The Observer:

/**
 * Prepend an additional order info box to the gift options block.
 * - event: core_block_abstract_to_html_after
 *
 * @param Varien_Event_Observer $observer
 */
public function insertAdminBlock(Varien_Event_Observer $observer)
{
    // >> check for adminhtml/sales_order/view route here <<

    $giftOptionsBlock = $observer->getBlock();
    if ($giftOptionsBlock->getNameInLayout() !== 'gift_options') {
        // not interested in other blocks than gift_options
        return;
    }

    $customInfoBlock = Mage::app()->getLayout()->createBlock(
        'adminhtml/template',
        'custom_order_info',
        array(
            'template' => 'mymodule/custom_order_info.phtml',
            'order' => Mage::registry('current_order'),
        )
    );

    $giftOptionsHtml = $observer->getTransport()->getHtml();
    $customHtml  = $customInfoBlock->toHtml();

    $observer->getTransport()->setHtml($customHtml . $giftOptionsHtml);
}

The Template:

<div class="entry-edit">
    <div class="entry-edit-head">
        <h4><?php echo Mage::helper('sales')->__('Custom Value') ?></h4>
    </div>
    <fieldset>
        <tr>

            <td class="label"><label><strong><?php echo Mage::helper('sales')->__('Custom Value:') ?></strong></label></td>
            <td class="value"><?php echo $this->getOrder()->getIncrementId() ?></td>
        </tr>
    </fieldset>
</div>

The Result:

enter image description here