Magento Enterprise – Sales Order Grid Can’t Export Custom Columns

exportmagento-enterpriseorder-gridsales-order

I've added the a few columns to the sales_flat_order_gridtable.
The values are obtained in these two events sales_order_resource_init_virtual_grid_columns (using addVirtualGridColumn() ) and sales_order_resource_update_grid_records (updating the table values for some more complex joins).

<add_order_grid_column_handle>
    <reference name="sales_order.grid">
        <action method="addColumnAfter">
            <columnId>product_names</columnId>
            <arguments>
                <header>Product(s) Name(s)</header>
                <index>product_names</index>
                <type>text</type>
            </arguments>
            <after>created_at</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>

However, when exporting a csv of the grid, it doesn't include the custom columns.

I found that the file is loaded using the grid block like this:

$this->getLayout()->createBlock('adminhtml/sales_order_grid');

But that, for some reason, is not loading the layout update I have written above, so the new columns are not used…

Is there any settings, or layout handle to add to this columns, so they are also used in the exported csv?

Best Answer

The grid export is a different controller action, so the layout handle would be different, something like adminhtml_sales_order_exportCsv (didn't check if this is the exact handle). However, exportCsvAction doesn't seem to call loadLayout so probably using layout updates here wouldn't work.

From what I could tell by briefly looking at the code, there are three options, none of them extremely clean:

  1. Create a custom grid extending Mage_Adminhtml_Block_Sales_Order_Grid and add your logic to the _prepareColumns and _prepareCollection methods, then replace the core grid with the custom one
  2. Overwrite the Mage_Adminhtml_Block_Sales_Order_Grid block and add your logic in _prepareColumns and _prepareCollection.
  3. Overwrite the Mage_Adminhtml_Sales_OrderController controller, exportCsvAction and call addColumn on the $grid block object to add your columns