The issue you’re facing is likely due to the way Magento handles multiple tracking numbers for an order. When you join the sales_flat_shipment_track table using group_concat in your collection preparation method, it concatenates all tracking numbers and titles for each order into a single field separated by commas.
However, when you try to filter this grouped column, Magento doesn’t handle multiple values gracefully. It treats each comma-separated value as an individual string rather than an array of strings. This is why you’re seeing duplicate entries and the filter not working as expected.
To resolve these issues, I suggest refactoring your approach:
- Modify your collection join to use a subquery that returns only one row per order with all tracking numbers and titles concatenated:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$this->setCollection($collection);
// Use a subquery to handle multiple tracking numbers per order
$collection->getSelect()->joinLeft(
array('track' => new Zend_Db_Expr("SELECT sales_flat_order.entity_id, " .
"GROUP_CONCAT(sales_flat_shipment_track.track_number SEPARATOR ', ') AS track_number, " .
"GROUP_CONCAT(sales_flat_shipment_track.title SEPARATOR ', ') AS title " .
"FROM `sales_flat_order` " .
"LEFT JOIN sales_flat_shipment_track ON (sales_flat_order.entity_id = sales_flat_shipment_track.order_id) " .
"GROUP BY sales_flat_order.entity_id"))
, 'main_table.entity_id = track.entity_id', array('track_number', 'title'));
return parent::_prepareCollection();
}
- Modify your column definitions to set the columns as text and use the correct index:
$this->addColumn('track_number', array(
'header' => Mage::helper('sales')->__('Track Number'),
'width' => '80px',
'type' => 'text',
'index' => 'track_number',
));
$this->addColumn('title', array(
'header' => Mage::helper('sales')->__('Title'),
'width' => '80px',
'index' => 'title',
));
- For filtering, you can use the existing grid filter functionality provided by Magento. The spaceSeparatedFilter method you tried is not needed in this case since we’re only joining a single row per order.
The modified code above should resolve both issues:
- Duplicate tracking numbers and titles will be shown as distinct values (e.g., 12345678,123456789).
- The filter for track_number and title columns will work correctly with the new group_concat implementation.
Regarding your second question about filtering in the sales/order grid: By default, Magento allows filtering of columns using the built-in grid filters. To enable filtering on the track_number and title columns, make sure you have defined them as shown above in your grid.php file, with the correct ‘index’ attribute.
When you click on a column header in the grid, it should display the filter form for that column. Enter the desired filter value (e.g., “Federal Express” or a specific tracking number) and click Apply to see the filtered results. The grid should update accordingly.
If you still face any issues with filtering after implementing the suggested changes, please let me know! I’d be happy to help further.