2

Usually we need to add to the Order Grid some column, so let take this as example.

There are 3 ways to do that:

  1. Rewrite the grid block in our custom module
  2. Use an observer on the event core_block_abstract_prepare_layout_before
  3. Use a Layout update

(let don't consider the alteration of the collection to make things more easy)

  • First method is the most powerful but It leads to conflict in the scenarios.
  • Second method is in IMHO resource consuming as we are adding an extra code step for every block we are going to render
  • Third step is a layout update so should be the most advisable ... but are we sure it will work always ?

Other consideration are welcome

References:
Method 1: - http://inchoo.net/ecommerce/magento/programming-magento/adding-customer-group-column-to-sales-grid-in-magento/
Method 2: - http://www.atwix.com/magento/add-column-to-customers-grid-alternative-way/
Method 3: - http://www.atwix.com/magento/column-to-orders-grid/

3 Answers 3

4

Best practice to add a column to the backend grid is of course implementation of event-observer method. But question arises which event is used for the purpose? And one of the Answer is core_block_abstract_to_html_before

Suppose say you have implemented observer model for above event with method coreBlockAbstractToHtmlBefore.

CODE:

public function coreBlockAbstractToHtmlBefore(Varien_Event_Observer $observer)
{

    /** @var $block Mage_Core_Block_Abstract */
    $block = $observer->getEvent()->getBlock();
    if ($block->getId() == 'customerGrid')
    {
        // Add the attribute as a column to the grid
        $block->addColumnAfter(
            'custom_grid_element',
            array(
                'header' => $helper->__('Custom Grid Element'),
                'align' => 'center',
                'width' => '80px',
                'type' => 'text',
                'options' => array(
                ),
                'default' => '',
                'index' => 'custom_grid_element',
                'renderer'  => ''
            ),
            'customer_since'
        );

        // Set the new columns order.. otherwise our column would be the last one
        $block->sortColumnsByOrder();
    }
}

Above is just an example for adding custom grid element to the Customer grid. You can deploy the very same technique for Sales grid as well.

3
  • Any reason to choose core_block_abstract_to_html_before instead of core_block_abstract_prepare_layout_before
    – Fra
    Commented May 21, 2013 at 5:12
  • Hope this helps: "Just as with the _prepareLayout method, the before and after html rendering methods have analogous global system events core_block_abstract_to_html_before core_block_abstract_to_html_after Unlike the lifecycle callbacks, these events will fire regardless of the cache state of a block. So, if you were trying to use _beforeToHtml but its cache behavior was problematic, setting a listener for core_block_abstract_to_html_before should get you where you need to go." REFER: alanstorm.com/magento_listener_lifecycle_block
    – MagePsycho
    Commented May 21, 2013 at 6:15
  • When using the "core_block_abstract_to_html_before" event for my observer, I was not able to do any sorting or filtering on my custom column. Instead I had to use the "core_block_abstract_prepare_layout_after" event, as described here: magento.stackexchange.com/a/4720/3830
    – Louis B.
    Commented Sep 12, 2016 at 12:08
3

If you want to use on observer try adminhtml_block_html_before instead of core_block_abstract_to_html_before

You could also use your module adminhtml layout.xml

    <?xml version="1.0"?>
    <layout>
    <add_order_grid_column_handle>
        <reference name="sales_order.grid">
            <action method="addColumnAfter">
                <columnId>magepal_internal_order_num</columnId>
                <arguments module="sales" translate="header">
                    <header>MagePal #</header>
                    <index>magepal_internal_order_num</index>
                    <type>text</type>
                    <width>110px</width>
                </arguments>
                <after>shipping_name</after>
            </action>
        </reference>
    </add_order_grid_column_handle>
    <adminhtml_sales_order_grid>
        <update handle="add_order_grid_column_handle" />
    </adminhtml_sales_order_grid>
    <adminhtml_sales_order_index>
        <update handle="add_order_grid_column_handle" />
    </adminhtml_sales_order_index>
    <layout>

See Add new field to administration orders grid

1
  • When using the "core_block_abstract_to_html_before" event for my observer, I was not able to do any sorting or filtering on my custom column. Instead I had to use the "core_block_abstract_prepare_layout_after" event, as described here: magento.stackexchange.com/a/4720/3830
    – Louis B.
    Commented Sep 12, 2016 at 12:09
2

The best way to add a column to the order grid is to create an observer for the sales_order_resource_init_virtual_grid_columns event. Just add a column to the sales_flat_order_grid table in your install script and use this event to store the data directly into the table at every order save. This way there is no need for any joins when showing the grid to the user. For more details on how to setup the column see http://www.ecomdev.org/2010/07/27/adding-order-attribute-to-orders-grid-in-magento-1-4-1.html

5
  • the event sales_order_resource_init_virtual_grid_columns is never triggered during order grid load
    – Fra
    Commented May 20, 2013 at 10:16
  • Thats correct, it's triggered at saving the order and copies to correct data to the sales_flat_order_grid table that is used when displaying the sales/order grid in the admin. Because all data is stored in a single database there is only a single select needed to collect the data (no joins that create heavy queries every time the grid is loaded). Commented May 20, 2013 at 17:15
  • If I just open the Order grid page it is not triggered
    – Fra
    Commented May 21, 2013 at 5:12
  • 1
    No, again the event is triggered at order save! It is used to generate to select/insert into the sales_flar_order_grid table. The Order grid page only displays the data and the collection is read from the sales_flat_order_grid table. Commented May 21, 2013 at 6:10
  • Any hint for customising also invoice grid and credit memo grid? Thanks.
    – ermannob
    Commented Sep 4, 2015 at 10:01

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.