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?
2 Answers
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
adminhtml_sales_order_view
- Set your block into the layout which will reference
order_giftmessage
block - In your block, overload method
_prepareLayout
add :
$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
-
Important note: This is only possible in Magento Enterprise, in CE there is no gift_message block. Feb 15, 2015 at 0:14
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
core_block_abstract_to_html_after
- In the observer, check if the block class is
Mage_Adminhtml_Block_Sales_Order_View_Tab_Info
if so, inject
$customBlock->toHtml()
into the result. You can try to find the right position withstr_replace
orpreg_replace
but I prefer using DOM manipulation:Find the shipping information div with the following XPath:
//h4[contains(@class,"head-shipping-method")]/../../..
- Insert new content after this.
What you should know about the
core_block_abstract_to_html_after
event 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