How to add a custom filter to the product grid in Magento 2?

Why do you need Magento 2 grid filters?

Magento 2 grid filters are used in situations when you have a large number of products in your store and it may be hard to manage them and find the needed product quickly. In this case, you can add Magento 2 inline edit grid with custom filters. Let’s see how you can do it.

When you have a large number of products in your store, it may be hard to manage them and find the needed product quickly. In this case, you can add Magento 2 inline edit grid with custom filters. Let’s see how you can do it.

Warning: The code below is an example of how it may look like and requires further customization according to your needs.

How to add the admin grid custom filter in Magento 2?

Step 1. To add the Magento 2 admin grid custom filter, create a custom plugin for your Magneto 2. Register it by creating the the app/code/Amasty/Testgrid/registration.php file: 

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Amasty_Testgrid',
    __DIR__
);


magento-2-custom-filters-on-the-product-grid


Step 2. The next step to add filter to the grid in Magento 2 is to create the app/code/Amasty/Testgrid/etc/module.xml file that is responsible for module installation and updates:

<?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="Amasty_Testgrid" setup_version="1.0.0">
    </module>
</config>

Step 3. To continue adding the Magento admin grid custom filter, you need to initialize the column in the UI component file app/code/Amasty/Testgrid/view/adminhtml/ui_component/product_listing.xml. It may look like this:

<?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" class="Magento\Catalog\Ui\Component\Listing\Columns">
        <column name="yearly_views" sortOrder="100">
            <settings>
                <addField>true</addField>
                <filter>textRange</filter>
                <label translate="true">Yearly views</label>
            </settings>
        </column>
    </columns>
</listing>

Step 4. The next step in creating magento 2 inline edit grid is to set up a column for the product grid in the app/code/Amasty/Testgrid/Ui/DataProvider/Product/AddYearlyViewsFieldToCollection.php file: 

<?php
namespace Amasty\Testgrid\Ui\DataProvider\Product;
use Magento\Framework\Data\Collection;
use Magento\Ui\DataProvider\AddFieldToCollectionInterface;
class AddYearlyViewsFieldToCollection implements AddFieldToCollectionInterface
{
    public function addField(Collection $collection, $field, $alias = null)
    {
        $collection->joinField(
            'yearly_views',
            'report_viewed_product_aggregated_yearly',
            'views_num',
            'product_id=entity_id',
            null,
            'left'
        );
    }
}

In this file, we can add to the collection the needed field with the help of the addField function. We connect the table ID and product ID. Here we’ve used a view statistics table.

Step 5. Continuing adding the Magento 2 admin inline edit grid. In the

app/code/Amasty/Testgrid/Ui/DataProvider/Product/AddYearlyViewsFilterToGrid.php file, we set up a custom filter for this column in the Magento 2 admin grid:

<?php
namespace Amasty\Testgrid\Ui\DataProvider\Product;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\Data\Collection;
use Magento\Ui\DataProvider\AddFilterToCollectionInterface;
class AddYearlyViewsFilterToGrid implements AddFilterToCollectionInterface
{
    public function addFilter(Collection $collection, $field, $condition = null)
    {
        if (isset($condition['gteq'])) {
            $collection->addFieldToFilter([[
                'attribute' => 'yearly_views', 'gteq' => $condition['gteq']]
            ]);
        }
        if (isset($condition['lteq'])) {
            $collection->addFieldToFilter([[
                'attribute' => 'yearly_views', 'lteq' => $condition['lteq']]
            ]);
        }
    }
}

Step 6. In the last step, we add functions addFilterStrategies and addFieldStrategies to the app/code/Amasty/Testgrid/etc/adminhtml/di.xml file:

<?xml version="1.0"?>
<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="yearly_views" xsi:type="object">
                    Amasty\Testgrid\Ui\DataProvider\Product\AddYearlyViewsFieldToCollection
                </item>
            </argument>
            <argument name="addFilterStrategies" xsi:type="array">
                <item name="yearly_views" xsi:type="object">
                    Amasty\Testgrid\Ui\DataProvider\Product\AddYearlyViewsFilterToGrid
                </item>
            </argument>
        </arguments>
    </type>
</config>

How to add Magento 2 custom admin module grid?

You can add Magento 2 custom admin module grid with inline filters for editing without coding. Install the Extended Product Grid with Editor extension and set up a convenient product grid view. The plugin allows you to filter products by any parameters you need and apply the filtering option to default and custom columns on your grid.

And that’s it. Now you know how to add a new filter to the Magento 2 product grid.

According to the statistics, you may find helpfull the following question about custom columns.

How can we help you?

Didn’t you find the answer to your question? We are always happy to help you out.

© 2009-2024 Amasty. All Rights Reserved.