I'm trying to find a way of inserting a small form into the adminhtml sales order view page, right above the order items block, without intervening in the template file. So, basically, is there a way of doing that exclusively via layout XML?
You can replace the gift message block with your own and append the output of the original gift message block. I have not tested but it should work:
- Use handle
- Set your block into the layout which will reference
- In your block, overload method
$giftMessageBlock = $this->getLayout()->getBlock('order_giftmessage); $this->setData('gift_html', $giftMessageBlock->toHtml()); $giftMessageBlock->setTemplate('YOUR TEMPLATE'); // dont forget to echo data gift_html
- And return parent method
- That's all
If you are using Magento EE, refer to the answer of GuillaumeD, which shows an halfway elegant way to include a custom block via layout.
For Magento CE you'll have to choose the lesser of two evils, i.e. modify the rendered HTML of the order_view_info block with an observer to not replace the whole template for this block:
- Observe the event
- In the observer, check if the block class is
if so, inject
$customBlock->toHtml()into the result. You can try to find the right position with
preg_replacebut I prefer using DOM manipulation:
Find the shipping information div with the following XPath:
- Insert new content after this.
What you should know about the
core_block_abstract_to_html_afterevent is, that you need to copy the output at the very beginning, otherwise it gets lost as soon as another block is rendered because they will share the same transport object:
$html = $observer->getTransport()->getHtml(); // should be the first statement // now you can do what you want, create other blocks, modifiy $html ... $observer->getTransport()->setHtml($html); // should be the last statement