Magento – Add a custom filter to search in magento2

filtermagento-2.0magento2search

I'm new with Magento2, I have a problem trying to add a new filter to the search module.

For example:

I created an attribute that names: "flag" of type "text field", that will be invisible in storefront.

I have 3 products:

name | description | flag

Product1 | Description1 | 1

Product2 | Description2 | 1

Product3 | Description3 | 0

When I type in the search box "product", by default Magento2 returns all the products, because all matches with the
search, what I want is add a filter to the search, that only returns the products with flag = 1, so it will returns only the product 1 and 2.

I don't know what is the exactly block or model that I must override.

Best Answer

Look for the grid's listing page like this. The product listing for regular catalog can be found here (it differs for configurable product etc). For your custom attribute's config, add

<item name="filter" xsi:type="string">text</item>

and the filter should show up on the header for that field.

Related Topic