Sales order grid Filter & Sorting not working in Magento

Sales order grid Filter & Sorting not working in Magento

August 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Sales order grid Filter & Sorting not working in Magento

Magento Sales Order Grid Filter & Sorting Not Working

As a senior developer specializing in e-commerce platforms like Magento, I often encounter issues related to grid filtering and sorting. In this post, we’ll troubleshoot the common problem of Magento sales order grid filter and sorting not working as expected.

The Problem

You’ve successfully added a new column for customer email to the sales order grid by rewriting the Mage_Adminhtml_Block_Sales_Order_Grid block in your local module. However, despite retrieving the email value correctly for each order, sorting and filtering this new column aren’t functioning properly.

Current Attempts

You’ve tried several approaches:

  1. Joining the necessary tables (sales_flat_order and sales_flat_order_address) in the setCollection method using the joinLeft function.
  2. Setting up the email column in the _prepareColumns method with appropriate configuration for sorting and filtering.

Reviewing the Code

Let’s analyze the code you’ve provided:

public function setCollection($collection)
{
    // ...

    $collection->getSelect()->joinLeft(
        array('sfo' => 'sales_flat_order'),
        'main_table.entity_id=' . 'sfo' . '.entity_id',
        array('*')
    );

    // ...
}
protected function _prepareColumns()
{
    // ...

    $this->addColumnAfter('email', array(
        // Email column configuration
    ), 'erp_confirm_order');

    return parent::_prepareColumns();
}

Identifying the Issue

The main issue seems to be with how you’re setting up the email column for sorting and filtering:

  • The filter_index is set incorrectly. It should reference the joined table’s field name, not the local table.
  • The order_callback and filter_condition_callback methods are missing proper SQL preparation.

To fix this issue, you need to adjust your SQL joins and prepare the sorting and filtering callbacks correctly:

  1. Update the joinLeft calls in the setCollection method:
$collection->getSelect()->joinLeft(
    array('sfo' => 'sales_flat_order'),
    'main_table.entity_id = sfo.entity_id',
    array('*')
);

$collection->getSelect()->joinLeft(
    array('sfoa' => 'sales_flat_order_address'),
    'main_table.entity_id = sfoa.parent_id',
    array('email')
);
  1. Update the filter_last_login and sort_last_login methods:
function filter_last_login($collection, $column)
{
    if (!$column->getFilter()->getCondition()) {
        return;
    }

    $condition = $collection->getConnection()
        ->prepareSqlCondition('sfoa.email', $column->getFilter()->getCondition());
    $collection->getSelect()->where($condition);
}

function sort_last_login($collection, $column)
{
    $collection->getSelect()->order($this->_getConditionColumnName($column), strtoupper($column->getDir()));
}
  1. Add a helper method to get the correct column name for sorting:
private function _getConditionColumnName($column)
{
    return 'sfoa.' . $column->getIndex();
}

Conclusion

By following these recommendations, you should be able to resolve the sales order grid filter and sorting issue. Remember that proper SQL join configuration and handling of joined table fields are crucial for correct filtering and sorting behavior.

If you’re still facing problems or need further guidance on Magento development, feel free to explore our Magento customization services at Laravel Company. Our team of experienced developers is ready to help you optimize your e-commerce platform’s performance and user experience.

Database Extensions Admin Configuration

Our network