5

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?

1

2 Answers 2

5
  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

1

You cannot change the type of an already instantiated block with $block->setType(). The type determines which class is used to instantiate the block and an instantiated block cannot change its class in PHP.

So if you want to change the type of a block, you will actually have to replace it with a new instance, for example:

$newBlock = $block->getLayout()->createBlock('new/type');
$block->getParent()->setChild($block->getAlias(), $newBlock);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.