I have made customer attribute 'sales_executive' which shows proper value in customer grid. Now i want to show the same value in sales order grid by adding column. How to add this column and get value in it?
2 Answers
You need to create a custom module:
app/code/local/My/Module/etc/config.xml
:
<?xml version="1.0"?>
<config>
<modules>
<My_Module>
<version>0.0.1</version>
</My_Module>
</modules>
<global>
<models>
<module>
<class>My_Module_Model</class>
</module>
</models>
</global>
<adminhtml>
<events>
<!-- To register the controller action -->
<controller_action_predispatch_adminhtml>
<observers>
<my_module_custom_register>
<type>singleton</type>
<class>module/observer</class>
<method>registerController</method>
</my_module_custom_register>
</observers>
</controller_action_predispatch_adminhtml>
<!-- Called after creating a block -->
<core_layout_block_create_after>
<observers>
<my_module_custom_before_block>
<type>singleton</type>
<class>module/observer</class>
<method>blockCreateAfter</method>
</my_module_custom_before_block>
</observers>
</core_layout_block_create_after>
<!-- Called before loading a non EAV collection -->
<core_collection_abstract_load_before>
<observers>
<my_module_custom_before_core_load_collection>
<class>module/observer</class>
<method>beforeCoreCollectionLoad</method>
</factoryx_custom_before_core_load_collection>
</observers>
</core_collection_abstract_load_before>
</events>
</adminhtml>
</config>
app/code/local/My/Module/Model/Observer.php
:
<?php
class My_Module_Model_Observer
{
public function registerController(Varien_Event_Observer $observer)
{
$action = $observer->getControllerAction()->getFullActionName();
switch ($action)
{
case 'adminhtml_sales_order_index':
case 'adminhtml_sales_order_grid':
case 'adminhtml_sales_order_exportCsv':
case 'adminhtml_sales_order_exportExcel':
Mage::register('adminhtml_sales_order_grid', true);
break;
}
return $this;
}
public function blockCreateAfter(Varien_Event_Observer $observer)
{
$block = $observer->getBlock();
if($block instanceof Mage_Adminhtml_Block_Sales_Order_Grid)
{
$block->addColumnAfter(
'accref',
array(
'header' => Mage::helper('sales')->__('Account Reference'),
'index' => 'accref',
'filter_index' => 'cust.value',
),
'created_at'
);
}
}
public function beforeCoreCollectionLoad(Varien_Event_Observer $observer)
{
if (Mage::registry('adminhtml_sales_order_grid')) {
$collection = $observer->getCollection();
if (!isset($collection)) {
return;
}
if ($collection instanceof Mage_Sales_Model_Resource_Order_Collection) {
// Get the attribute id for the accref attribute
$eavAttribute = Mage::getModel('eav/entity_attribute');
$accrefId = $eavAttribute->getIdByCode('customer', 'accref');
$collection->getSelect()->joinLeft(
array('cust' => Mage::getSingleton("core/resource")->getTableName('customer_entity_varchar')),
'main_table.customer_id = cust.entity_id AND cust.attribute_id = '.$accrefId,
array(
'cust.value'
)
);
}
And obviously:
app/etc/modules/My_Module.xml
:
<?xml version="1.0"?>
<config>
<modules>
<My_Module>
<active>true</active>
<codePool>local</codePool>
</My_Module>
</modules>
</config>
EDIT
Using _prepareCollection()
you can view this link
-
Don't want to create observer for it. If you can help me by function _prepareCollection() in grid.php in sales/order? Commented Dec 9, 2016 at 11:02
-
check my updated answer. or checkout this magento.stackexchange.com/questions/104336/…. Commented Dec 9, 2016 at 11:05
-
@chirag I am using this module github.com/yvoronoy/magento-extension-extra-fee, the extra fees field value applied but the field column not visible backend order total, may i know whats is the error?– zusCommented Sep 9, 2020 at 14:34
You will achieve this with the help of renderer.
You just need to addColum
in your order grid and pass the renderer class in that. and create that renderer class page wherever you want.
You can perform/load any value there and return whatever you want to show in grid.
You can get the help from this example.