Adding Custom Block to Magento Sales Order View
Adding a custom block to the sales order view in Magento admin involves several steps that require modifying XML files and creating new PHP classes. In this guide, we’ll walk you through the process step by step.
Step 1: Create the Module Configuration File
First, create or update your module configuration file located at app/etc/modules/Webspeaks_MyModule.xml. The contents should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Webspeaks_MyModule>
<active>true</active>
<codePool>local</codePool>
</Webspeaks_MyModule>
</modules>
</config>
Step 2: Configure the Layout Update
Next, update your module’s configuration file at app/code/local/Webspeaks/MyModule/etc/config.xml to include layout updates and event observers. Here’s an example:
<?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>
Step 3: Create the Observer Class
Create a new observer class at app/code/local/Webspeaks/MyModule/Model/Observer.php to handle the event triggered in the config.xml file:
<?php
class Webspeaks_MyModule_Model_Observer {
public function getSalesOrderViewInfo(Varien_Event_Observer $observer) {
$block = $observer->getBlock();
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);
}
}
}
}
Step 4: Add the Custom Block to the Layout
Create a new layout XML file at app/design/adminhtml/default/default/layout/mymodule.xml to specify where your custom block should be placed:
<?xml version="1.0"?>
<layout version="1.0">
<adminhtml_sales_order_view>
<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>
Step 5: Create the Custom Block Template
Create a new PHTML file at app/design/adminhtml/default/default/template/mymodule/custom.phtml to define the content of your custom block:
<?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>
Step 6: Create the Custom Block Class
Finally, create a new block class at 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;
}
}
Conclusion
Following these steps, you should now have a custom block added to the sales order view in your Magento admin panel. This guide demonstrates how to create and configure a module, update layout files, create observer and block classes, and add the necessary templates.
Remember to replace Webspeaks_MyModule with the actual name of your module throughout these files. Also, ensure that your theme’s cache is disabled during development and flushed after making changes for them to take effect.
For more information on Magento development best practices and advanced techniques, feel free to explore our website.