In this article, we will Update Categories in Product Grid in Magento 2.
We will also add a filter for this column.
For the sake of this article, we will add a “Categories” column to product grid.
That will show which categories the product belongs to, but you can add any column you want.
You just need to populate the column with relevant data.
Now we have successfully added the “Categories” column to the product grid. Also, note that this column is filterable.
Creating New Modules
First thing’s first, create a new module.
For this tutorial, we will create a module named RLTS_Helloworld.
Then create the file app/code/RLTS/Helloworld/view/adminhtml/ui_component/product_listing.xml.
Put the following code in this file:
And the following line specifies the unique id for the column, in this case, category_id. It also specifies a Category class that will populate this column.
The following line specifies a CategoryList class which will populate the filter for this column. In Magento 2, filters are shown separately at top of product grid.
RLTS\Helloworld\Model\Category\Categorylist
The following line specifies that in filters, the field will appear as a dropdown (select input).
select
The following line determines the label of the field in the product grid.
Categories
Creating New Files
The following file will have the code that will actually populate the column with data.
app/code/RLTS/Helloworld/Ui/Component/Listing/Column/Category.php
In our case, we are creating categories. You can populate with any data you want.
Simply put in the following code in this file.
Now create the following file app/code/RLTS/Helloworld/Model/Category/Categorylist.php.
This file will have the code that will fill Categories dropdown in filters.
Put the following code in this file:
Finally, we need to override a UI Data provider.
Create the following file app/code/RLTS/Helloworld/etc/di.xml
And put the following code:
Also, create the following file app/code/RLTS/Helloworld/Ui/DataProvider/Product/ProductDataProvider.php.
Put the following code in this file:
Hence, we have successfully added the Magento “Categories” column to the product grid.