sulman
sulman

Reputation: 2461

Magento - re write admin sales order grid to add custom column from DB field

I am trying to extend the Mage_Adminhtml_Block_Sales_Order_Grid to add a new column. I have a populated field in the 'sales_flat_order' table (order_ready_for_dispatch) that I wish to show in the grid (this field is added/updated separately). The issue is the data is not showing in that column with the following code:

/app/code/local/Sulman/SalesGrid/etc/config.xml

<?xml version="1.0"?>
<config>    
  <global>
    <modules>
        <Sulman_SalesGrid>
            <version>1.0.0</version>
        </Sulman_SalesGrid>
    </modules>
    <blocks>
        <adminhtml>
            <rewrite>
                <sales_order_grid>Sulman_SalesGrid_Block_Adminhtml_Sales_Order_Grid</sales_order_grid>
            </rewrite>
        </adminhtml>
    </blocks>        
  </global>       
</config>   

app/code/local/Sulman/SalesGrid/Block/Adminhtml/Sales/Order/Grid.php

<?php

class Sulman_SalesGrid_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{

    protected function _prepareCollection() {
        $collection = parent::_prepareCollection()->getCollection();
        $collection->addFieldToSelect('order_ready_for_dispatch');
        return $this;
    }

    protected function _prepareColumns()
    {
        $this->addColumn('order_ready_for_dispatch', array(
            'header'=> Mage::helper('sales')->__('Despatch Date'),
            'width' => '80px',
            'type'  => 'text',
            'index' => 'order_ready_for_dispatch',
        ));

        return parent::_prepareColumns();
    }
}

Can anyone see my mistake? Thanks :)

Upvotes: 1

Views: 1430

Answers (1)

sulman
sulman

Reputation: 2461

Ok so I got it working by altering my _prepareCollection() method:

protected function _prepareCollection(){
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $collection->getSelect()->join('sales_flat_order', 'main_table.entity_id = sales_flat_order.entity_id', array('order_ready_for_dispatch'));
    $this->setCollection($collection);
    return $this;
}

Upvotes: 2

Related Questions