Customizing Magento Collections: Adding Delivery Dates to the Sales Order Grid
As an experienced Magento developer, I often encounter scenarios where clients need to extend the default grid functionality to include custom data. In this case, you’re trying to display delivery dates from a third-party table in the sales order grid. The approach you’ve taken so far is commendable, but I’ll guide you through a more efficient and maintainable solution.
Understanding Your Current Approach
You’re attempting to override the _prepareCollection() method in Mage/Adminhtml/Block/Sales/Order/Grid.php to fetch data directly from the database using a custom SQL query. This approach has some limitations:
- It tightly couples your grid block with the specific table structure, making it difficult to maintain or extend.
- It doesn’t leverage Magento’s built-in collection functionality, which provides features like pagination, sorting, and filtering.
Introducing Magento’s Collection Interface
Magento 2 introduced a more robust collection system that allows you to easily incorporate custom data without directly querying the database. To add your delivery dates, we’ll need to create a new collection class that extends Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult.
First, let me share an example of how you can extend this interface:
class DeliveryDateCollection extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
{
protected $_joinFields = [
'aw_deliverydate_delivery' => 'mage_aw_deliverydate_delivery',
];
public function _initSelect()
{
parent::_initSelect();
$this->getSelect()->join(
['aw_deliverydate_delivery' => $this->getTable('mage_aw_deliverydate_delivery')],
'main_table.entity_id = aw_deliverydate_delivery.order_id',
['delivery_date']
);
}
}
In this example, we’re defining a join field and using the _initSelect() method to add our left join. This collection will return all orders along with their corresponding delivery dates.
Registering Your Custom Collection
To use your custom collection, you must register it as an observer for the ui_component_data_source event. Create a new plugin in an appropriate module:
class DeliveryDateCollectionPlugin
{
protected $collectionFactory;
public function __construct(\Magento\Framework\View\Element\UiComponent\DataProvider\SearchResultFactory $collectionFactory)
{
$this->collectionFactory = $collectionFactory;
}
public function afterGetData(UiComponentDataProvider $subject, $result)
{
if ($subject->getRequest()->getParam('component') === 'sales_order_grid') {
return $this->collectionFactory->create(['searchResultClass' => DeliveryDateCollection::class]);
}
return $result;
}
}
In this plugin, we’re checking if the requested component is the sales order grid. If it is, we create a new instance of our custom collection and return it.
Updating Your Grid Configuration
The last step involves updating your grid configuration file (usually located in app/code/Vendor/Module/etc/adminhtml/di.xml) to reference your new plugin:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
<plugin disabled="false">
<type name="Vendor_Module\Plugin\DeliveryDateCollectionPlugin"/>
</plugin>
</type>
</config>
Adding the Delivery Date Column
Finally, you can add the delivery date column to your grid definition in app/code/Vendor/Module/view/adminhtml/ui_component/sales_order_grid.xml:
<column name="delivery_date">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Delivery Date</item>
<item name="adminhtml_widget_config" xsi:type="string">Magento\Backend\Block\Widget\Grid\Column\Date</item>
<item name="header_css_class" xsi:type="string">col-date</item>
</item>
</argument>
</column>
Conclusion
By following this approach, you’ve successfully added delivery dates to your sales order grid without directly querying the database. This method is more flexible and maintainable, as it separates your grid definition from the data retrieval logic.
Remember, when working with Magento, it’s essential to leverage the built-in interfaces and event system to ensure your code remains modular and easily extendable.
For more information on customizing Magento grids, I recommend checking out our comprehensive guide on LaravelCompany’s blog. It provides detailed walkthroughs of best practices for extending grid functionality in Magento 2.
Happy coding!