Adding more columns with custom data to the Magento base grid is likely the most given task for all Magento programmers. Because of that, we are going to learn how to add columns to the grid by joining tables. These custom columns will render data from a custom joined table. This blog will show you how to add a custom column to product grid in Magento 2 and explain how it works.

Here, we will create a custom table whose data will be added to the product listing grid.

We assume we all know how to create a basic module, so let’s just get to the main point, shall we? Here are three simple steps to add another column to the Magento grid.

Step 1: Create a table

In order to show whichever data we want, we’ll store it in our own table. Here, we’ll simply call it magenest_custom_column  with only two columns, id and value.

$installer->getTable('magenest_custom_column')
)
   ->addColumn(
       'id',
       Table::TYPE_INTEGER,
       null,
       [
           'identity' => true,
           'nullable' => false,
           'primary' => true,
           'unsigned' => true,
       ],
       'Increment ID'
   )
   ->addColumn(
       'value',
       Table::TYPE_TEXT,
       100,
       ['nullable => false'],
       'Custom Column Value'
   )
   ->setComment('Custom Column Table');
$installer->getConnection()->createTable($table);

Step 2: Join our table with our wishlist table

We’ll join the product table by extending Product Collection through injection.

<type name="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider">
   <arguments>
       <argument name="collectionFactory" xsi:type="object">produdct_grids</argument>
   </arguments>
</type>

<virtualType name="produdct_grids" type="Magento\Catalog\Model\ResourceModel\Product\CollectionFactory">
   <arguments>
       <argument name="instanceName" xsi:type="string">\Magenest\Grids\Model\Grid\JoinProductCollection</argument>
   </arguments>
</virtualType>

Inside JoinProductCollection class is where the action takes place. Here, we’ll extend ProductCollection and join our custom table with the product table through _initSelect function.

class JoinProductCollection extends ProductCollection
{
   public function _initSelect()
   {
       parent::_initSelect();
       $this->addFilterToMap('value,'custom_column');
       $this->getSelect()->join(['second_table' => 'magenest_custom_column'],'e.entity_id = second_table.id','second_table.value);
       return $this;
   }

Step 3: Add column using ui_component

After all the hard work, the last thing we need to do now is to add our column to the grid. Based on whichever grid you want, you can add your custom column just by creating a UI component file with the exact same name as a grid file. Particularly, to execute this, we only need to place these UI component files inside our module view/adminhtml/ui_component folder:

Finally, we’ll create product_listing.xml for product grid:

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
   <columns name="product_columns">
       <column name="custom_column">
           <argument name="data" xsi:type="array">
               <item name="config" xsi:type="array">
                   <item name="sortOrder" xsi:type="number">999</item>
                   <item name="filter" xsi:type="string">text</item>
                   <item name="label" translate="true" xsi:type="string">Product Custom Column</item>
               </item>
           </argument>
       </column>
   </columns>
</listing>

After that, clear cache and go to the admin site and see the magic for yourself. The result should look like this.

Add custom column to product grid on Magento 2 backend

Conclusion

There are multiple ways to add a custom column to the Magento 2 product grid. In this blog, We’ve shown you a simple way by joining a custom table with your own data. Hope this helps and good luck.