销售订单网格过滤器&;在Magento中排序不起作用

销售订单网格过滤器&;在Magento中排序不起作用,magento,magento-1.7,magento-1.9,Magento,Magento 1.7,Magento 1.9,我想在Magento销售订单网格中显示客户电子邮件。我必须重写本地模块中的Mage\u Adminhtml\u Block\u Sales\u Order\u Grid,以添加客户电子邮件的新列。我得到了每个订单的电子邮件价值,但排序和过滤没有按预期工作 我花了一天多的时间整理这个问题,但没有运气。此外,我在SO中也提到了一些答案 下面是我尝试过的代码 编辑1 当我对客户电子邮件列进行排序和筛选时,什么都不起作用,当我单击其他默认网格列时,也会出现错误 Integrity constraint

我想在Magento销售订单网格中显示客户电子邮件。我必须重写本地模块中的
Mage\u Adminhtml\u Block\u Sales\u Order\u Grid
,以添加客户电子邮件的新列。我得到了每个订单的电子邮件价值,但排序和过滤没有按预期工作

我花了一天多的时间整理这个问题,但没有运气。此外,我在SO中也提到了一些答案

下面是我尝试过的代码

编辑1

当我对客户电子邮件列进行排序和筛选时,什么都不起作用,当我单击其他默认网格列时,也会出现错误

Integrity constraint violation: 1052 Column 'increment_id' in order clause is ambiguous, query was: SELECT `main_table`.*, `sfo`.*, `sfoa`.`email` FROM `sales_flat_order_grid` AS `main_table`
 LEFT JOIN `sales_flat_order` AS `sfo` ON main_table.entity_id=sfo.entity_id
 LEFT JOIN `sales_flat_order_address` AS `sfoa` ON main_table.entity_id=sfoa.parent_id GROUP BY `main_table`.`entity_id` ORDER BY increment_id ASC LIMIT 20
任何帮助,我都非常感激。
谢谢,

因此,您面临的问题与原始答案不同,就是您加入的字段名称不明确。您需要指定要筛选的字段。我以前也遇到过这种情况

当您从sales_flat_order添加列时,不需要执行自定义筛选或排序回调。只有当您需要一些更复杂的过滤/排序查询时,才有必要这样做,如带有最后登录日期的原始示例。只要确保设置了“过滤器索引”,它就会正常工作。下面的示例用于从帐单地址添加客户电子邮件

class My_Namespace_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
    public function setCollection($collection)
    {
        $collection->join(['sfoa' => 'sales/order_address'], 'main_table.entity_id=sfoa.parent_id AND sfoa.address_type="billing"', ['billing_email' => 'sfoa.email']);

        parent::setCollection($collection);
    }

    public function _prepareColumns()
    {
        parent::_prepareColumns();

        foreach ($this->getColumns() as $column) {
            if (!$column->getFilterIndex()) {
                $column->setFilterIndex('main_table.'.$column->getIndex());
            }
        }

        $this->addColumnAfter('customer_email', [
            'header' => Mage::helper('customer')->__('Customer Email'),
            'width'  => '50px',
            'index'  => 'billing_email',
            'filter_index' => 'sfoa.email',
        ], 'shipping_name');

        $this->sortColumnsByOrder();
        return $this;
    }
}
以下是订单表本身的一个示例:

class My_Namespace_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
    public function setCollection($collection)
    {
        $collection->join(['sfo' => 'sales/order'], 'main_table.entity_id=sfo.entity_id', 'customer_email');

        parent::setCollection($collection);
    }

    public function _prepareColumns()
    {
        parent::_prepareColumns();

        foreach ($this->getColumns() as $column) {
            if (!$column->getFilterIndex()) {
                $column->setFilterIndex('main_table.'.$column->getIndex());
            }
        }

        $this->addColumnAfter('customer_email', [
            'header' => Mage::helper('customer')->__('Customer Email'),
            'width'  => '50px',
            'index'  => 'customer_email',
            'filter_index' => 'sfo.customer_email',
        ], 'shipping_name');

        $this->sortColumnsByOrder();
        return $this;
    }
}

您是否收到任何错误消息,或者它根本不起任何作用?@Ian在进行排序和筛选时不会发生任何事情。请检查编辑1。谢谢
class My_Namespace_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
    public function setCollection($collection)
    {
        $collection->join(['sfo' => 'sales/order'], 'main_table.entity_id=sfo.entity_id', 'customer_email');

        parent::setCollection($collection);
    }

    public function _prepareColumns()
    {
        parent::_prepareColumns();

        foreach ($this->getColumns() as $column) {
            if (!$column->getFilterIndex()) {
                $column->setFilterIndex('main_table.'.$column->getIndex());
            }
        }

        $this->addColumnAfter('customer_email', [
            'header' => Mage::helper('customer')->__('Customer Email'),
            'width'  => '50px',
            'index'  => 'customer_email',
            'filter_index' => 'sfo.customer_email',
        ], 'shipping_name');

        $this->sortColumnsByOrder();
        return $this;
    }
}