0

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?

3
  • Which version are you using? Dec 9, 2016 at 10:53
  • Magento 1.9x version Dec 9, 2016 at 10:54
  • is your issue cleared?
    – zus
    Jan 12, 2018 at 5:21

2 Answers 2

1

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

Adding custom sales order attribute to order grid

3
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.