Search
×
Log in or create an account
If you have an account with us, please log in.

You have no items in your shopping cart.

Frequently Asked Questions

How can we help you?

banner image

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

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.

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

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


Helpful tip: You can add Magento 2 admin 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.

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


Step 2. Then 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. Next, 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. Then, 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. 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>

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.

Rate the answer?

Total rating: 5.0

If you didn’t find the answer on your question, please, ask here:

Back to top