2

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.

Thanks in advance

ramses
  • 505
  • 4
  • 8

1 Answers1

2

You can add this attribute under search form as a hidden field with the value you want to filter with. When user click on the search button it would perform the search by adding one more parameter in the query string i.e ?q=product&flag=1 and you will get the expected result. With this way you don't need to overwrite any block or controller. But you need to overwrite the default search form. Below URL can help you overwriting core magento files.

Reference URL: https://magento.stackexchange.com/questions/84550/magento-2-override-base-view-file

Note: This technique will not work with the auto search. In that case you need to overwrite core search blocks.

Community
  • 1
  • 1
Akshay Jindal
  • 365
  • 2
  • 6
  • Thanks Akshay for your answer, I would like that users can't see in the query that option, any smart user can change the parameter value and the query will return all products – ramses Mar 03 '16 at 14:33
  • That was helpful, thanks! Just saying, its possible to filter for multiple values by adding them like this with a comma: `` – user4095519 Dec 06 '22 at 08:53