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 Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

10 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

7 days ago