Admin Order Grid overridden with custom fields cannot be filtered

The question:

I have an issue with an overridden admin grid (sales order grid), which contains custom fields.

Those fields can be filtered, without any issue, but, when i try to filter by OrderId, an error is thrown :

SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘increment_id’ in where clause is ambiguous

How can i specify the tablename before the column?

This is how i have overridden my collection :

app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());

    $collection->getSelect()->join(
        array("sales" => 'sales_flat_order'), 
        "main_table.entity_id = sales.entity_id", 
        array('customer_email'=>'sales.customer_email',
        'coupon_code'=>'sales.coupon_code',
        'coupon_rule_name'=>'sales.coupon_rule_name',
        'shipping_description'=>'sales.shipping_description',
        'biebersdorf_customerordercomment'=>'sales.biebersdorf_customerordercomment')
    )
        ->join(
        array("customer_info" => 'customer_entity_varchar'), 
        "main_table.entity_id = customer_info.entity_id ", 
        array('value' => 'customer_info.value')
    )
    ->where('customer_info.attribute_id = 562')
    ->join(
        array("customer_group_info" => 'customer_entity'), 
        "main_table.entity_id = customer_group_info.entity_id ", 
        array('group_id' => 'customer_group_info.group_id')
    );
$collection->printLogQuery(true);                   

$this->setCollection($collection);
return parent::_prepareCollection();
    }

EDIT :

protected function _prepareColumns()
{

    $this->addColumn('real_order_id', array(
        'header'=> Mage::helper('sales')->__('Order #'),
        'width' => '80px',
        'type'  => 'text',
        'index' => 'increment_id',
    ));


    /* if (!Mage::app()->isSingleStoreMode()) {
        $this->addColumn('store_id', array(
            'header'    => Mage::helper('sales')->__('Purchased From (Store)'),
            'index'     => 'store_id',
            'type'      => 'store',
            'store_view'=> true,
            'display_deleted' => true,
        ));
    } */

    $this->addColumn('created_at', array(
        'header' => Mage::helper('sales')->__('Date commande'),
        'index' => 'created_at',
        'type' => 'datetime',
        'width' => '100px',
    ));

    $this->addColumn('billing_name', array(
        'header' => Mage::helper('sales')->__('Bill to Name'),
        'index' => 'billing_name',
    ));

    $this->addColumn('shipping_name', array(
        'header' => Mage::helper('sales')->__('Ship to Name'),
        'index' => 'shipping_name',
    ));

            /* CUSTOM FIELDS*/

            $this->addColumn('customer_email', array(
        'header'=> Mage::helper('customer')->__('Email'),
        'width' => '80px',
        'type'  => 'text',
        'index' => 'customer_email',
    ));

            $this->addColumn('etablissement', array(
                    'header'=> Mage::helper('customer')->__('Etablissement'),
        'width' => '80px',
        'type'  => 'text',
        'index' => 'value',
            ));

             $this->addColumn('group_id', array(
        'header'=> Mage::helper('customer')->__('Groupe'),
        'width' => '80px',
        'index' => 'group_id',
                    //'type' => 'text',
                    'renderer' => new Mage_Adminhtml_Block_Sales_Order_Renderer_CustomerGroup(),
                    'type' => 'options',
                    'options' => Mage_Adminhtml_Block_Sales_Order_Renderer_CustomerGroup::getCustomerGroupsArray(), 
    ));  

            $this->addColumn('coupon_rule_name', array(
        'header'=> Mage::helper('customer')->__('code réduction'),
        'width' => '80px',
        'type'  => 'text',
        'index' => 'coupon_rule_name',
    ));

            $this->addColumn('shipping_method', array(
        'header'=> Mage::helper('sales')->__('Livraison (Méthode)'),
        'width' => '80px',
        'type'  => 'text',
        'index' => 'shipping_description',
    ));

            $this->addColumn('biebersdorf_customerordercomment', array(
        'header'=> Mage::helper('customer')->__('Commentaire client'),
        'width' => '80px',
        'type'  => 'text',
        'index' => 'biebersdorf_customerordercomment',
                    'filter'    => false,
        'sortable'  => false,
    ));

            /* CUSTOM FIELDS */

    $this->addColumn('base_grand_total', array(
        'header' => Mage::helper('sales')->__('G.T. (Base)'),
        'index' => 'base_grand_total',
        'type'  => 'currency',
        'currency' => 'base_currency_code',
    ));

    $this->addColumn('grand_total', array(
        'header' => Mage::helper('sales')->__('G.T. (Purchased)'),
        'index' => 'grand_total',
        'type'  => 'currency',
        'currency' => 'order_currency_code',
    ));

    $this->addColumn('status', array(
        'header' => Mage::helper('sales')->__('Status'),
        'index' => 'status',
        'type'  => 'options',
        'width' => '70px',
        'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
    ));

    if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
        $this->addColumn('action',
            array(
                'header'    => Mage::helper('sales')->__('Action'),
                'width'     => '50px',
                'type'      => 'action',
                'getter'     => 'getId',
                'actions'   => array(
                    array(
                        'caption' => Mage::helper('sales')->__('View'),
                        'url'     => array('base'=>'*/sales_order/view'),
                        'field'   => 'order_id',
                        'data-column' => 'action',
                    )
                ),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
                'is_system' => true,
        ));
    }
    $this->addRssList('rss/order/new', Mage::helper('sales')->__('New Order RSS'));

    $this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV'));
    $this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel XML'));

    return parent::_prepareColumns();
}

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Hi Just add filter_index in increament_id field and it may works

        'filter_index' => 'main_table.increment_id',

and now code

$this->addColumn('real_order_id', array(
    'header'=> Mage::helper('sales')->__('Order #'),
    'width' => '80px',
    'type'  => 'text',
    'index' => 'increment_id',
    'filter_index' => 'main_table.increment_id',

));


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment