Magento: Filtering Order/Invoice Collections After Join and Custom Actions
As an experienced e-commerce developer, I often encounter challenges when working with complex systems like Magento. Recently, a client asked about filtering order/invoice collections after performing joins and custom actions in the back-end grid display.
Let’s break down this issue step-by-step and find a solution that maintains pagination and ordering functionality.
Understanding the Problem
The client has identified the correct files to modify:
- app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php
- app/code/core/Mage/Adminhtml/Block/Sales/Invoice/Grid.php
However, when modifying the _prepareCollection() function and adding a custom join for payment methods, the pagination and ordering features break.
The code provided attempts to retrieve an array of active payment methods and map their labels to the order collection. Although this works initially, it causes issues with Magento’s built-in data processing.
The Culprit: Direct Manipulation of the Collection
The problem lies in directly manipulating the collection after performing joins and before returning it to the parent function. When you iterate over the collection and set custom data (in this case, method labels), it disrupts the internal state of the collection that Magento uses for pagination and sorting.
The Solution: Using Varien_Data_Collection_Db
To resolve this issue without breaking built-in Magento functionality, we should use the Varien_Data_Collection_Db class instead of directly manipulating the collection. This class provides a consistent way to handle collections in Magento while preserving pagination and ordering capabilities.
Here’s an updated version of the _prepareCollection() function using Varien_Data_Collection_Db:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->getCollectionClass())
->addAttributeToSelect('*');
// Perform joins as before
$collection->getSelect()->joinLeft(
'sales_flat_order',
'main_table.entity_id = sales_flat_order.entity_id',
array('total_qty_ordered', 'shipping_description')
);
$collection->getSelect()->joinLeft(
'sales_flat_order_payment',
'sales_flat_order_payment.parent_id = main_table.entity_id',
array('method')
);
// Create a new Varien_Data_Collection_Db instance
$paymentMethods = Mage::getSingleton('core/resource')->getConnection('core_read');
// Execute the custom query to retrieve payment methods
$methodsQuery = "SELECT `value`, `label` FROM sales_order_payment_method WHERE is_active=1";
$methodsResult = $paymentMethods->fetchAll($methodsQuery);
// Iterate over the collection and set method labels using Varien_Data_Collection_Db
foreach ($collection as &$item) {
$methodLabel = '';
foreach ($methodsResult as $method) {
if ($method['value'] == $item['method']) {
$methodLabel = $method['label'];
break;
}
}
// Use Varien_Data_Collection_Db to set the custom data
$item->setData('method_label', $methodLabel);
}
$this->setCollection($collection);
return parent::_prepareCollection();
}
Key Changes Made
- We create a new Varien_Data_Collection_Db instance using Magento’s resource model.
- Instead of iterating over the collection and setting data directly, we use the
Varien_Data_Collection_Dbinstance to perform the mapping between payment methods and their labels. - The custom data (
method_label) is set for each item in the collection using Varien_Data_Collection_Db’s built-in functionality.
Conclusion
By following this approach, you can successfully filter order/invoice collections after performing joins and custom actions without breaking pagination and ordering functionality in Magento.
Remember to apply these changes across both the Order and Invoice grid files (Sales_Order and Sales_Invoice) and clear your cache to ensure proper rendering of the updated grid display.
If you’re looking for more advanced Magento solutions or need assistance with your e-commerce development projects, feel free to reach out to our team at Laravel Company. We specialize in providing top-notch e-commerce development services tailored to meet your business needs.