How To

How to Filter by Multiple SKU on Admin Product Grid in Magento 2?

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.

Steps to Filter by Multiple SKU on Admin Product Grid in Magento 2:

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);
        }
    }
}

Conclusion:

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!

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

3 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago