Blog: Thinking Out Loud

Musings on (Magento) Life, the (.NET) Universe and Everything (IT)

Now we have successfully added “Categories” column to product grid and also this column is filterable. In this article we will add a custom column to product grid. 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.

Creating New Module:

First create a new module. For this tutorial we create a module RLTS_Helloworld. Then create file: app/code/RLTS/Helloworld/view/adminhtml/ui_component/product_listing.xml. Put following code in this file:

The following line

specifies the unique id for 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 magento2, filters are shown separately at top of product grid.

The following line:

specifies that in filters, the field will appear as a dropdown (select input).

The following line:

determines the label of the field in product grid.

Creating Files:

app/code/RLTS/Helloworld/Ui/Component/Listing/Column/Category.php
This file will have the code that will actually populate the column with data. In our case we are getting categories. You can populate with any data you want. Put in following code in this file.

Now create following file:

app/code/RLTS/Helloworld/Model/Category/Categorylist.php

This file will have the code that will fill Categories dropdown in filters.

Put following code in this file:

Finally we need to override a Ui Dataprovider.

Create following file:
app/code/RLTS/Helloworld/etc/di.xml
Put following code

Also create following file:
app/code/RLTS/Helloworld/Ui/DataProvider/Product/ProductDataProvider.php

Put following code in this file:

Finally,  we have successfully added “Categories” column to product grid and also this column is filterable.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">