Order Grid – Integrity Constraint Violation: 1052 Column ‘base_grand_total’ in Where Clause is Ambiguous

order-grid

I've been able to add a few custom attributes to the sales order grid (sku, custom name). I am able to filter correctly. When I add the customer email, I can add and filter it too, but then if I filter by a total (e.g. base grand total) I get the error above. Other solutions suggest using filter_index to remove this ambiguity but this hasn't helped, presumably because it's on a field I'm not adding (grand total) – so this is not due to ambiguous entity_id. How do I make it use the specific total field?

Here are the helpful parts:

Observer

 class Kaska_ExtendedGrid_Model_Observer
{

public function salesOrderGridCollectionLoadBefore($observer)
{

    $collection = $observer->getOrderGridCollection();
    $select = $collection->getSelect();

    $select->join('m_sales_flat_order', '`m_sales_flat_order`.entity_id = `main_table`.entity_id', array('email'  => new Zend_Db_Expr('`m_sales_flat_order`.customer_email')));

    $select->joinLeft(array('payment' => $collection->getTable('sales/order_payment')), 'payment.parent_id=main_table.entity_id', array('payment_method' => 'method'));

    $select->join('m_sales_flat_order_item','`m_sales_flat_order_item`.order_id=`main_table`.entity_id', array('sku' => new Zend_Db_Expr('`m_sales_flat_order_item`.sku'),'name'  => new Zend_Db_Expr('`m_sales_flat_order_item`.name') ));

    $select->join('m_catalog_product_entity_varchar', '`m_catalog_product_entity_varchar`.attribute_id=163 AND `m_catalog_product_entity_varchar`.entity_id = `m_sales_flat_order_item`.`product_id`', array('attr'  => new Zend_Db_Expr('`m_catalog_product_entity_varchar`.value')));

    $select->group('main_table.entity_id');


}


public function filterEmail($collection, $column)
{

    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }
    $collection->getSelect()->having(
        "`m_sales_flat_order`.customer_email like ?", "%$value%");
    return $this;
}


.... filterSku etc

layout.xml

<sales_order_grid_update_handle>
    <reference name="sales_order.grid">
        <action method="addColumnAfter">
            <columnId>payment_method</columnId>
            <arguments>
                <header>Payment Method</header>
                <index>payment_method</index>
                <filter_index>payment.method</filter_index>
                <type>text</type>
            </arguments>
            <after>shipping_name</after>
        </action>
        <action method="addColumnAfter">
            <columnId>email</columnId>
            <arguments helper="kaska_extendedgrid/getEmailColumnParams" />
            <after>payment_method</after>
        </action>

Helper file

public function getEmailColumnParams(){
    return array(
    'header'    => 'Customer Email',
    'index'     => 'email',
    'type'        => 'text',
    'filter_index'=>'m_sales_flat_order.customer_email',
}

I also tried locally overriding the _prepareColumns of Mage/Adminhtml/Block/Sales/Order/Grid.php

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

EDIT
After some help from below, the culprit was another extension's overriding of the order grid preparecolumns

Best Answer

Try with this by using Zend_Db_Expr, not sure it will fix.

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