Here, we will see how to add stock management column in product grid Magento 2.
It’s easy to add a column to the products grid if a value that you want to display in the column is a product attribute. They are already present in the products grid toolbar under Columns control.
You just check/uncheck columns that you want to display or remove from the grid. But what if you want to display values like quantity and website that aren’t product attribute?
Because stock management can be turned on/off per product, we will add a new column to the products grid that will display if stock management is turned on or off.
Steps to add stock management column in product grid Magento 2
For all of the code examples, I created a Magento 2 module, named Webkul_Custom.
If you want to learn how to create a Magento 2 module, you can check this: How to create a basic module in Magento 2
It’s important that Magento’s Magento_Catalog module is loaded before our module, that’s why we will add it under sequence tag in the module.xml file.
app/code/Webkul/Custom/etc/module.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Webkul_Custom" > <sequence> <module name="Magento_Catalog"/> </sequence> </module> </config>
To render products grid Magento 2 uses Listing UI component instance named product_listing and XML configuration file for it. We need to create a file in our module with the same path and name.
app/code/Webkul/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="80"> <settings> <label translate="true">Manage Stock</label> <addField>true</addField> <options class="Magento\Config\Model\Config\Source\Yesno"/> <filter>select</filter> <dataType>select</dataType> <sortable>false</sortable> </settings> </column> </columns> </listing>
To add new column we need to reference columns UI component named product_columns from the original config file and add our new column to it. We named our column manage_stock with label Manage Stock.
First, we will create a field strategy that will add stock management status to the products collection.
app/code/Webkul/Custom/Ui/DataProvider/Product/AddManageStockFieldToCollection.php
<?php namespace Webkul\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' ); } }
For times when someone wants to filter grid data by stock management status, we need to create this filtering strategy.
app/code/Webkul/Custom/Ui/DataProvider/Product/AddManageStockFilterToCollection.php
<?php namespace Webkul\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); } } }
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.
app/code/Webkul/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">Webkul\Custom\Ui\DataProvider\Product\AddManageStockFieldToCollection</item> </argument> <argument name="addFilterStrategies" xsi:type="array"> <item name="manage_stock" xsi:type="object">Webkul\Custom\Ui\DataProvider\Product\AddManageStockFilterToCollection</item> </argument> </arguments> </type> </config>
After that, install the module properly and see the results as showing in below screenshot.
Thank you for reading this article on How to add stock management column in product grid Magento 2, we hope you liked it.
You can also view our wide range of ready-to-use Magento 2 extensions.
Be the first to comment.