Magento 1.9 – Unable to Search Orders by Custom Column in Admin Order Grid

fatal errormagento-1.9order-grid

I had created 4 custom fields for the Order:

pgs_order_website_names
pgs_aramex_reference
pgs_aramex_shipped
pgs_aramex_shipped_date

I had copied Grid.php to local/Mage/

class Mage_Adminhtml_Block_Sales_Order_Grid extends
Mage_Adminhtml_Block_Widget_Grid

Constructor

public function __construct()
{
    parent::__construct();
    $this->setId('sales_order_grid');
    $this->setUseAjax(true);
    $this->setDefaultSort('created_at');
    $this->setDefaultDir('DESC');
    $this->setSaveParametersInSession(true);
}

The _prepareCollection() Method :

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $select = $collection->getSelect();
    $select->joinLeft(array(
                        'order' => Mage::getModel('core/resource')->getTableName('sales/order')),
                        'order.entity_id=main_table.entity_id',
                        array(
                        'pgs_order_website_names' => 'pgs_order_website_names',
                        'pgs_aramex_reference'=>'pgs_aramex_reference',
                        'pgs_aramex_shipped'=>'pgs_aramex_shipped',
                        'pgs_aramex_shipped_date'=>'pgs_aramex_shipped_date'
                        )
                    ); 
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

The _prepareColumns() Method

protected function _prepareColumns()
{
    $this->addColumn('pgs_order_website_names', array(
        'header'=> Mage::helper('sales')->__('Ordered Websites'),
        'index' => 'pgs_order_website_names',
        'width' => '80px',
        'type'  => 'text',
        'filter' => false,

    ));
    $this->addColumn('pgs_aramex_reference', array(
        'header'=> Mage::helper('sales')->__('Aramex ref. No'),
        'index' => 'pgs_aramex_reference',
        'width' => '80px',
        'type'  => 'text',
        'filter' => false,

    ));
    $this->addColumn('pgs_aramex_shipped', array(
        'header'=> Mage::helper('sales')->__('Sent to Warehouse ?'),
        'index' => 'pgs_aramex_shipped',
        'width' => '80px',
        'type'  => 'text',
        'filter' => false,

    ));
    $this->addColumn('pgs_aramex_shipped_date', array(
        'header'=> Mage::helper('sales')->__('Sent to Warehouse ?'),
        'index' => 'pgs_aramex_shipped_date',
        'width' => '80px',
        'type'  => 'date',
        'filter' => false,
    ));
return parent::_prepareColumns();

}

Everythig is working fine if i make the 'filter' attribute to false in $this->addColumn() Method! But if i changed to true im getting an error :

Fatal error: Call to a member function setColumn() on a non-object
The column attributes are :

$dateAttribute  = array(
    'type'          => 'date',
    'backend_type'  => 'date',
    'frontend_input' => 'date',
    'is_user_defined' => true,
    'label'         => 'Shipped date',
    'visible'       => true,
    'required'      => false,
    'user_defined'  => false,   
    'searchable'    => false,
    'filterable'    => true,
    'comparable'    => false,

);

Magento CE 1.9

Best Answer

Try to use Alias in your join section, like below :

$select->joinLeft(array(
                    'order' => Mage::getModel('core/resource')->getTableName('sales/order')),
                    'order.entity_id=main_table.entity_id',
                    array(
                    'pgs_order_website_names' => 'order.pgs_order_website_names',
                    'pgs_aramex_reference'=>'order.pgs_aramex_reference',
                    'pgs_aramex_shipped'=>'order.pgs_aramex_shipped',
                    'pgs_aramex_shipped_date'=>'order.pgs_aramex_shipped_date'
                    )

EDIT : Also use column names with Aliases like 'filter_index'=>'main_table.increment_id' in addColumn method e.g.

 $this->addColumn('order_id', array(
'header' => Mage::helper('sales')->__('Order Id'),
'align' =>'left',
'index' => 'increment_id',
'filter_index'=>'main_table.increment_id',

));

Related Topic