Solving Custom Column Filter Issues in Magento 2 Sales Order Grid
Hello, I’m BienHV from LaravelCompany, and today we’re going to solve an issue that many of us face when filtering custom columns in the Magento 2 sales order grid.
The Current Setup
BienHV has already set up a custom column called “Purchase Point” in the sales_order_grid.xml file. This column displays the country name associated with each order. Here’s how it looks:
<column name="purchase_from" class="MyApp\Sales\Ui\Component\Listing\Column\Order\PurchaseFrom">
<settings>
<label translate="true">Purchase Point</label>
<bodyTmpl>ui/grid/cells/html</bodyTmpl>
<sortable>false</sortable>
<filter>select</filter>
<options class="MyApp\Sales\Ui\Component\Listing\Column\Order\PurchaseFrom\Options" />
<dataType>select</dataType>
</settings>
</column>
BienHV has also created the app/code/MyApp/Sales/Ui/Component/Listing/Column/Order/PurchaseFrom.php file to populate this column with country names using the prepareDataSource() function:
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
$fieldName = $this->getData('name');
foreach ($dataSource['data']['items'] as &$item) {
$orderId = $item['entity_id'];
$item[$fieldName] = $this->getCountryName($orderId);
}
}
return $dataSource;
}
And an options class app/code/MyApp/Sales/Ui/Component/Listing/Column/Order/PurchaseFrom/Options.php for filtering.
The Problem
When BienHV tries to apply a filter using this custom column, the grid fails with no error message, showing a popup saying “Attention Something went wrong.” and the notice: “Something went wrong with processing the default view and we have restored the filter to its original state.”
The issue lies in the current implementation not adhering to Magento 2’s data component structure for filters.
The Solution
To resolve this, we need to ensure that our custom column setup is compatible with Magento 2’s built-in filtering system. Here are the steps:
- Check if the column name matches the database field: Ensure your
$fieldNameinprepareDataSource()corresponds to a valid field in the sales_order table or its join tables.
$item[$fieldName] = $this->getCountryName($orderId);
- Use Magento 2’s built-in filtering mechanism for custom columns:
- Implement the
Magento\Ui\Component\Listing\Columns\ColumnInterfaceinterface in your column class. - Override the
prepareMeta()method to define your filter options and add them to the meta object. - Override the
applyFilter()method to handle the filtering logic based on the selected option.
- Implement the
Here’s an example of how you might implement this:
class PurchaseFrom extends \Magento\Ui\Component\Listing\Columns\Column implements ColumnInterface
{
// ... existing code ...
public function prepareMeta(array $dataSource)
{
$meta = $this->meta->getData();
$meta['filter_groups'][] = [
'filters' => [
[
'type' => 'select',
'label' => __('Country'),
'value' => 0,
'input' => 'select',
'data_type' => 'select',
'options' => $this->getCountryOptions(),
],
],
'name' => 'country_filter'
];
return $meta;
}
public function applyFilter(array $dataSource, array $filter)
{
// Your filter logic here
}
}
- Update your options class to comply with Magento 2’s filtering:
- The
getCountryOptions()method should return an associative array where the keys are option values and the values are labels.
- The
public function getCountryOptions()
{
$options = [];
foreach ($this->countryCollection as $country) {
$options[$country->getId()] = $country->getName();
}
return $options;
}
Conclusion
The key to resolving the filter issue lies in properly integrating your custom column with Magento 2’s core data components and filtering system. By adhering to the interface requirements of Magento\Ui\Component\Listing\Columns\ColumnInterface and updating your options class, you can ensure that your custom columns work seamlessly with the built-in grid filters.
Remember, the devil is in the details! Ensuring the column name matches the database field and handling filtering in the required methods will save you time debugging issues like this one.
If you found this solution helpful, feel free to share it on our Magento 2 Development Community for further discussion. We’re always here to help!
Happy coding!