I coded my event observer to add custom column to my sales order grid and still do not display the column to my website's admin panel? This code is for customizing magento sales order grid. Here is my code:
app/code/community/Mjsi/Orderprint/etc/config.xml
<adminhtml>
---
<events>
<core_layout_block_create_after>
<observers>
<mjsi_orderprint_add_columns>
<class>Mjsi_Orderprint_Model_Observer</class>
<method>addColumnsToGrid</method>
</mjsi_orderprint_add_columns>
</observers>
</core_layout_block_create_after>
<core_block_abstract_prepare_layout_before>
<observers>
<orderprint_core_block_abstract_prepare_layout_before>
<class>Mjsi_Orderprint_Model_Observer</class>
<method>addMassAction</method>
</orderprint_core_block_abstract_prepare_layout_before>
</observers>
</core_block_abstract_prepare_layout_before>
<adminhtml_widget_container_html_before>
<observers>
<mjsi_orderprint>
<class>Mjsi_Orderprint_Model_Observer</class>
<method>adminhtmlWidgetContainerHtmlBefore</method>
</mjsi_orderprint>
</observers>
</adminhtml_widget_container_html_before>
<events>
---
</adminhtml>
app/code/community/Mjsi/Orderprint/Model/Observer.php
<?php
class Mjsi_Orderprint_Model_Observer
{
/**
* Add new column to orders grid
*
* @param Varien_Event_Observer $observer
* @return $this
*/
public function addColumnsToGrid(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();
// Check whether the loaded block is the orders grid block
if (!($block instanceof Mage_Adminhtml_Block_Sales_Order_Grid)
|| $block->getNameInLayout() != 'sales_order.grid'
) {
return $this;
}
// Add a new column rigth after the "Status" column
$block->addColumnAfter('mjsi_orderprint', [
'header' => $block->__('Printed'),
'width' => '80px',
'filter' => false,
'renderer' =>'Mjsi_Orderprint_Block_Adminhtml_Sales_Order_Renderer_Printed',
//'index' => 'coupon_rule_name',
], 'status');
return $this;
}
public function addMassAction(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();
if(get_class($block) =='Mage_Adminhtml_Block_Widget_Grid_Massaction'
&& $block->getRequest()->getControllerName() == 'sales_order')
{
$block->addItem('massPrintSalesOrders_order', array(
'label' => 'Print Sales Orders',
'url' => Mage::app()->getStore()->getUrl('*/*/massPrintSalesOrders'),
));
}
}
public function adminhtmlWidgetContainerHtmlBefore($event) {
$block = $event->getBlock();
if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) {
$block->addButton('order_print', array(
'label' => Mage::helper('sales')->__('Print Orders'),
'onclick' => 'setLocation(\'' . $block->getUrl('*/*/print') . '\')',
));
}
}
}
Ok, for adding a new column to sales grid you need to add in config this event:
<sales_order_grid_collection_load_before>
<observers>
<mjsi_orderprint_add_columns>
<model>your_model/observer</model>
<method>addGridColumnsToSelect</method>
</mjsi_orderprint_add_columns>
</observers>
</sales_order_grid_collection_load_before>
then in observer you need to add in main collection your column, I added a custom field orygin_type from order table:
public function addGridColumnsToSelect(Varien_Event_Observer $observer)
{
$collection = $observer->getOrderGridCollection();
$orderTable = Mage::getSingleton('core/resource')->getTableName('sales_flat_order');
$collection->getSelect()
->joinLeft(
['order_table' => $orderTable],
"main_table.entity_id = order_table.entity_id",
[
'origin_type' => 'order_table.origin_type',
]
);
return $this;
}
after that in your layout xml loaded in config , if you don't have you need to add `
<adminhtml>
<layout>
<updates>
<your_model>
<file>your_file.xml</file>
</your_model>
</updates>
</layout>
</adminhtml>
and in app\design\adminhtml\default\default\layout\your_file.xml add this:
<?xml version="1.0" encoding="UTF-8"?>
<layout>
<sales_order_grid_update_handle_origin_type>
<reference name="sales_order.grid">
<action method="addColumnAfter">
<columnId>origin_type</columnId>
<arguments module="sales" translate="header">
<header>Origin Type</header>
<index>origin_type</index>
<type>text</type>
<width>100</width>
<filter_index>order_table.origin_type</filter_index>
</arguments>
<after>base_subtotal</after>
</action>
</reference>
</sales_order_grid_update_handle_origin_type>
</layout>
Hope it help!