1

I have created a module with the following observer that should pull the 'orderstatus_status_code' custom attribute value from the sales_flat_order table through to the Sales Order Grid View;

<?php    
    class OrderSync_Model_Observer {    
        /**
         * Adds column to admin sales order grid
         *
         * @param Varien_Event_Observer $observer
         * @return OrderSync_Model_Observer
         */
        public function appendOrderSync($observer)
        {
            $block = $observer->getBlock();
            if (!isset($block)) {
                return $this;
            }

            if ($block->getType() == 'adminhtml/sales_order_grid') {
                /* @var $block Mage_Adminhtml_Block_Sales_Order_Grid */
                $block->addColumnAfter('ordersync_status_code', array(
                    'header'    => 'Order Sync Status',
                    'type'      => 'varchar',
                    'index'     => 'ordersync_status_code',
                    'filter_index'     => 'ordersync_status_code',
                ), 'status');
            }
        }
    }

This has correctly added the 'Order Sync Status' column but no value is displaying where I know there is data in the table and I wondered if anyone can help to show me how to get the data there?

  • What Magento version? – dev_67Commerce Dec 6 '17 at 14:32
  • Apologies, CE 1.9.1.0 – doubleplusgood Dec 6 '17 at 14:51
  • Have you created filed "ordersync_status_code" only table sales_flat_order rigjht? – Abdul Dec 7 '17 at 6:52
  • Please create same field in table "sales_flat_order_grid" and use your code – Abdul Dec 7 '17 at 6:55
1

Declare 2 observers in your config.xml file: one to add your column to the grid block and the other one to load data from the corresponding attribute:

<adminhtml>
    <events>
        <core_block_abstract_to_html_before>
            <observers>
                <{observer_name}>
                    <type>singleton</type>
                    <class>{namespace}_{module}/observer</class>
                    <method>beforeBlockToHtml</method>
                </{observer_name}>
            </observers>
        </core_block_abstract_to_html_before>
        <eav_collection_abstract_load_before>
            <observers>
                <{observer_name}>
                    <class>{namespace}_{module}/observer</class>
                    <method>beforeCollectionLoad</method>
                </{observer_name}>
            </observers>
        </eav_collection_abstract_load_before>
    </events>
</adminhtml>

For your observer class-

class {Namespace}_{Module}_Model_Observer
{
    public function beforeBlockToHtml(Varien_Event_Observer $observer)
    {
        $grid = $observer->getBlock();

        /**
         * Mage_Adminhtml_Block_Customer_Grid
         */
        if ($grid instanceof Mage_Adminhtml_Block_Customer_Grid) {
            $grid->addColumnAfter(
                '{column_code}',
                array(
                    'header' => Mage::helper('{Module}_customer')->__('{{column_name}}'),
                    'index'  => '{column_code}'
                ),
                'entity_id'
            );
        }
    }

    public function beforeCollectionLoad(Varien_Event_Observer $observer)
    {
        $collection = $observer->getCollection();
        if (!isset($collection)) {
            return;
        }

        /**
         * Mage_Customer_Model_Resource_Customer_Collection
         */
        if ($collection instanceof Mage_Customer_Model_Resource_Customer_Collection) {
            /* @var $collection Mage_Customer_Model_Resource_Customer_Collection */
            $collection->addAttributeToSelect('{attribute_code}');
        }
    }
}

Review following URL - Adding columns to customer grid using observer or overriding the customer grid

Check answer by Arnaud AKA Narno

If you will the shared URL solves your query, you can close this question as it can have possible chances of getting closed due to Duplicate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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