Displaying Custom Order Attributes in the Magento Sales Order Grid
In Magento, adding custom attributes to the sales_order index grid involves modifying the order grid block and ensuring your attribute data is properly indexed for display. Let’s walk through this process step-by-step.
Adding a New Column to the Sales Order Grid
First, you need to create an extension to rewrite the existing sales_order_grid block. This is where we’ll add our new column definition.
app/code/local/namespace/module/etc/config.xml
<global>
<blocks>
<adminhtml>
<rewrite>
<sales_order_grid>Namespace_Module_Block_Sales_Order_Grid</sales_order_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
Next, we’ll define our custom block that extends the Mage_Adminhtml_Block_Sales_Order_Grid class.
app/code/local/namespace/module/Block/Sales/Order/Grid.php
class Namespace_Module_Block_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
protected function _prepareColumns()
{
$this->addColumn('custom_column', array(
'header' => $this->__('Custom Column'),
'index' => 'custom_attribute',
));
return parent::_prepareColumns();
}
}
In this example, replace 'custom_column' with the name of your new column and 'custom_attribute' with the name of the custom order attribute you wish to display.
Ensuring Data Availability for the New Column
By default, Magento doesn’t automatically include custom attributes in its pre-cached flat tables. To make your data available, you need to extend the Mage_Sales_Model_Resource_Order_Collection class and add your attribute to the select statement.
app/code/local/namespace/module/Model/Observer.php
class Namespace_Module_Model_Observer
{
public function addCustomAttributeToSelect(Varien_Event_Observer $observer)
{
$collection = $observer->getCollection();
$collection->addFieldToFilter('entity_id', array('gt' => 0));
return $this;
}
}
Configure this observer to run before the sales_order_grid_collection_load_before event. The observer will ensure your custom attribute is included in every order collection used for grid display.
Updating Grid Records After Attribute Changes
If you’re updating the value of your custom attribute after an order has been saved (e.g., via a manual process), you’ll need to update the grid records manually as well. This can be done using updateGridRecords() function provided by Magento.
$order->setCustomAttribute('custom_attribute', 'new_value');
$order->save();
$grid = Mage::getSingleton('adminhtml/sales_order_grid');
$grid->updateGridRecords($order);
This ensures the changes to your custom attribute are reflected in the grid display immediately after the update.
Conclusion
Displaying custom order attributes in the Magento sales order grid requires a combination of extending the order grid block, ensuring data availability through collection modifications, and potentially manually updating grid records. By following these steps, you can successfully add new columns to the sales order index grid for better management of your store’s orders.
Remember, it’s important to test your changes thoroughly to ensure they don’t disrupt other functions in your Magento installation. If you’re looking for more insights on customizing Magento modules and attributes, be sure to check out our extensive guides at LaravelCompany.