4
votes

I've added a custom attribute to the sales_order_flat table, and I'm trying to display this attribute in the grid at sales_order/index.

I've done this with customer attributes successfully, but I'm getting errors when trying the same thing with order data.

<global>
    <blocks>
        <adminhtml>
            <rewrite>
                <sales_order_grid>WACI_AdminHtmlExt_Block_Sales_Order_Grid</sales_order_grid>
            </rewrite>
        </adminhtml>
    </blocks>
 <!-- etc  -->

app/code/local/namespace/module/Block/Sales/Order/Grid.php

class WACI_AdminHtmlExt_Block_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{

    protected function _prepareColumns()
    {

        $this->addColumn('dynamics_ord', array(
            'header'    => $this->__('Dynamics ID'),
            'width'     => '75px',
            'index'     => 'dynamics_ord',
        ));

        $this->addColumnsOrder('dynamics_ord','entity_id');

        return parent::_prepareColumns();
    }
}

I see that _prepareCollection is sometimes used, but I'm trying to just amend the collection during setCollection for, if nothing else, simplicity.

Whatever the case, the column displays, but no data is produced. (And the page errors out when I try to sort).

Any ideas on this?

Update

This post is an extension of this SO thread I've updated it to match the modules current status. That is, I'm adding columns to both sales_flat_order and sales_flat_order_grid.

From that post, I've got an action that takes place to set this attribute value (after the order has been completed).

My question at this point is - how does updateGridRecords() come into play?

=> updateGridRecords appears to be called automatically when the $order->save() gets called (something I'm doing manually after the order is complete).

If I understand this correctly, because I'm not pulling data from any other model, the _prepareColumns call is all that's required.

=> yep. Displaying and sorting properly after the attr is saved. Win!

2

2 Answers

4
votes

The difference between customer collection and order grid collection is that customer collection is derived from Mage_Eav_Model_Entity_Collection_Abstract and sales_flat_order_grid collection is derived from Mage_Sales_Model_Resource_Abstract which is not EAV.

Because of that you can't add attributes to select like you would normaly be able to do with EAV - all the attributes that you want to use must be represented as a table column in the flat table.

The function addAttributeToSelect that you are calling is implemented in Mage_Sales_Model_Resource_Collection_Abstract class and uses in background addFieldToSelect so in you case you should have a column with name dynamics_ord in sales_flat_order_grid table.

The function that moves attribute values from order EAV to order grid is Mage_Sales_Model_Resource_Order_Abstract::updateGridRecords and this function reads the structure of sales_flat_order_grid table to know which attributes should be written to sales_flat_order_grid table so if dynamics_ord column is missing you will have to add it in your module's install script.

I came across this stack overflow solution on how to add the column in install script and then add it to the grid. The answer is a bit outdated but I think it could work.

I have also found this out of the box solution on how to add an attribute that will be used on orders grid but haven't tested it so I can't be certain if it works.

0
votes

When you make a join EAV table with FLAT table then you can not filter the data in grid. I had faced same problem earlier and had invested 4 days in that with my seniors, then finally come 2 conclusion that you can not do that....!!!