If you want to display a value in the column, it’s easy to add columns to the product grid. Those columns already exist in the grid toolbar under the control. You just check / uncheck the columns you want to display or have been removed from the grid. But what if you want to display values ​​like quantity and website which are not product attributes? If you continue to read, you will learn how to add value to this kind of grid.

Introduction

Since the stock management can be turned on / off per product, we will add a new column to the product grid which will show up when stock management is turned on or off.
For all code examples, I created a Magento 2 module, named Gurubhyo_Custom.

If you want to learn how to make Magneto 2 modules, then you can check: How to create the basic module in Magento 2

It is important that Magento Magento_Catalog module has been loaded before our module, which is why we will add it under the Sequence tags in module.xml file.

app/code/Gurubhyo/Custom/etc/module.xml

<?xml version="1.0" encoding="utf-8" ?>
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Gurubhyo_Custom" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

After editing module.xml, we need to update app/etc/config.php file with the console command:

php bin/magento setup:upgrade

Product listing UI component instance

To present the product grid, Magneto 2 uses a listing UI component example called Product UI List and XML Configuration File, for this Magento_Catalog / view / adminhtml / ui_component / product_listing.xml. If we want to customize it then we need to create a file in the same module with the same path and name.

app/code/gurubhyo/Custom/view/adminhtml/ui_component/product_listing.xml

<?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="manage_stock" component="Magento_Ui/js/grid/columns/select" sortOrder="76">
            <settings>
                <addField>true</addField>
                <options class="Magento\Config\Model\Config\Source\Yesno"/>
                <filter>select</filter>
                <dataType>select</dataType>
                <sortable>false</sortable>
                <label translate="true">Manage Stock</label>
            </settings>
        </column>
    </columns>
</listing>

To add a new column, we need to refer to the column UI named product_columns from the original configuration file and add our new column to it. We named our column manage_stock with label managed stock.

Run this console command to clean cache:

php bin/magneto cache:clean config

After clearing the cache, we should see our new column in the product grid but the data is missing. That’s because we have not added stock management data to product collection.

Product listing data provider

The best location to add stock management data to product collection is in the data provider class used by the product_listing UI component instance.

Magento_Catalog/view/adminhtml/ui_component/product_listing.xml

<dataSource name="product_listing_data_source" component="Magento_Ui/js/grid/provider">
    <settings>
        <storageConfig>
            <param name="dataScope" xsi:type="string">filters.store_id</param>
        </storageConfig>
        <updateUrl path="mui/index/render"/>
    </settings>
    <aclResource>Magento_Catalog::products</aclResource>
    <dataProvider class="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider" name="product_listing_data_source">
        <settings>
            <requestFieldName>id</requestFieldName>
            <primaryFieldName>entity_id</primaryFieldName>
        </settings>
    </dataProvider>
</dataSource>

If we look at the dataSource UI component we will see that the dataProvider class is Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider.

If we look at this data provider class, then we will see two properties addFieldStrategies and addFilterStrategies. Both of them are array with additional strategies which we can do on the collection of a specific column. They are called in the data provider’s adfilled and adfile methods, respectively. We will prepare our strategy for our stock management columns.

Manage stock field strategy

First of all, we will prepare a field strategy that will add stock management status to the product collection.

Gurubhyo\Custom\Ui\DataProvider\Product\AddManageStockFieldToCollection

<?php
namespace Gurubhyo\Custom\Ui\DataProvider\Product;
 
class AddManageStockFieldToCollection implements \Magento\Ui\DataProvider\AddFieldToCollectionInterface
{
    public function addField(\Magento\Framework\Data\Collection $collection, $field, $alias = null)
    {
        $collection->joinField(
            'manage_stock',
            'cataloginventory_stock_item',
            'manage_stock',
            'product_id=entity_id',
            null,
            'left'
        );
    }
}

In AddManageStockFieldToCollection field strategy we implemented addField method from Magento\Ui\DataProvider\AddFieldToCollectionInterface interface. Stock management status comes from the manage_stock column in cataloginventory_stock_item DB table. We used this information and Magento\Eav\Model\Entity\Collection\AbstractCollection::joinFieldmethod to add Stock management status to product collection.

In the AddManageStockFieldToCollection field strategy, we have implemented addField method from         Magento \ Ui \ DataProvider \ AddFieldToCollectionInterface interface. The stock management status comes from the manage_stock column in the cataloginventory_stock_item DB table. We used this information and                          Magento \ Eav \ Model \ Entity \ Collection \ AbstractCollection :: joinFieldmethod to add stock management status to product archive.

Manage stock filter strategy

Many times when a stock management wants to filter the grid data by state of the situation,                                                we need to create this filtering strategy.

Gurubhyo\Custom\Ui\DataProvider\Product\AddManageStockFilterToCollection

<?php
namespace Gurubhyo\Custom\Ui\DataProvider\Product;
 
class AddManageStockFilterToCollection implements \Magento\Ui\DataProvider\AddFilterToCollectionInterface
{
    public function addFilter(\Magento\Framework\Data\Collection $collection, $field, $condition = null)
    {
        if (isset($condition['eq'])) {
            $collection->addFieldToFilter($field, $condition);
        }
    }
}

In AddManageStockFilterToCollection filter strategy we implemented addFilter method from Magento\Ui\DataProvider\AddFilterToCollectionInterface. If grid is filterd by stock management status we will forward filter condtition to Magento\Eav\Model\Entity\Collection\AbstractCollection::addFieldToFilter method.

In AddManageStockFilterToCollection Filter Strategy, we have implemented addFilter method from          Magento \ Ui \ DataProvider \ AddFilterToCollectionInterface. If the grid is filtered by the stock management status, we will forward the filter condition to Magento \ Eav \ Model \ Entity \ Collection \ AbstractCollection :: addFieldToFilter method.

Add strategies to the product listing data provider

If we want to use just created strategies we need to add them to the product_listing data provider. We will do that in the di.xml file.

If we want to use only the strategies created, then we need to add them to the product_listing data provider. We will do this in the di.xml file.

app/code/Gurubhyo/Custom/etc/adminhtml/di.xml

<?xml version="1.0" encoding="utf-8" ?>
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider">
        <arguments>
            <argument name="addFieldStrategies" xsi:type="array">
                <item name="manage_stock" xsi:type="object">Gurubhyo\Custom\Ui\DataProvider\Product\AddManageStockFieldToCollection</item>
            </argument>
            <argument name="addFilterStrategies" xsi:type="array">
                <item name="manage_stock" xsi:type="object">Gurubhyo\Custom\Ui\DataProvider\Product\AddManageStockFilterToCollection</item>
            </argument>
        </arguments>
    </type>
</config>

Run this console command to clean cache:

php bin/magneto cache:clean config

After clearing  cache we should look at our new column in the product grid with the data.