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:
- Joining the necessary tables (sales_flat_order and sales_flat_order_address) in the
setCollectionmethod using thejoinLeftfunction. - Setting up the email column in the
_prepareColumnsmethod 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_indexis set incorrectly. It should reference the joined table’s field name, not the local table. - The
order_callbackandfilter_condition_callbackmethods are missing proper SQL preparation.
Recommended Solution
To fix this issue, you need to adjust your SQL joins and prepare the sorting and filtering callbacks correctly:
- Update the
joinLeftcalls in thesetCollectionmethod:
$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')
);
- Update the
filter_last_loginandsort_last_loginmethods:
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()));
}
- 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.