Hello Magento Friends,
Today I am going to throw light on How to Filter by Multiple SKU on Admin Product Grid in Magento 2?
While working with the admin product grid in Magento 2, you can filter only one SKU at a time. When admin users need to perform mass actions on the product then first they need to filter the products one by one and then apply mass actions. It becomes a time-consuming process for the admin. Thus, to make the work of the admin quick, I have a solution for it.
Note: For tracking all the admin activities install Magento 2 Admin Actions Log.
With the help of the below steps, the admin can filter multiple SKUs at a time from the admin product grid.
Step 1: Create a di.xml file in your extension
app\code\Vendor\Extension\etc\di.xml
Now, add the code as follows
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider" type="Vendor\Extension\Model\ProductDataProvider" /> </config>
Step 2: Now create the ProductDataProvider.php file in the following path
app\code\Vendor\Extension\Model\ProductDataProvider.php
Then add the code as mentioned below
<?php namespace Vendor\Extension\Model; class ProductDataProvider extends \Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider { /** * @inheritdoc */ public function addFilter(\Magento\Framework\Api\Filter $filter) { if (isset($this->addFilterStrategies[$filter->getField()])) { $this->addFilterStrategies[$filter->getField()] ->addFilter( $this->getCollection(), $filter->getField(), [$filter->getConditionType() => $filter->getValue()] ); } elseif ($filter->getField() == "sku" && count(explode(",",str_replace("%","",$filter->getValue()))) > 1) { $withComma = explode(",",str_replace("%","",$filter->getValue())); $attrs = array(); foreach ($withComma as $cItem) { $attrs[] = ['attribute' => $filter->getField(), $filter->getConditionType() => '%'.trim($cItem).'%']; } $this->getCollection()->addAttributeToFilter($attrs); } else { parent::addFilter($filter); } } }
Hence, this way you can filter multiple SKUs on the admin product grid in Magento 2.
Add any custom filter to Product Grid in Magento 2
If you face any difficulty, let me know through the comment box. Share the article with your friends and stay updated with us for more tutorials.
Happy Coding!
Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…
MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…
View Comments
Thank you.
This worked great, nice feature with very little work.