Magento – Changing advanced search results query – Magento 2

advanced-searchattributeslayered-navigationmagento2products

I've extended the default layered navigation in magento 2 so that filter parameters are added like so:

?free_from=304_303_302

Where option ids are separated by an underscore, in the above example the product list displays all products with one or more of the 3 options selected.

This allows users to add more that one filter in each filterable category.

My question is how do i extend the way the advanced search builds the advanced search results query, so that it matches the way the layered navigation applies filters.

Currently, when I select multiple options from the advanced search and select search, it adds the query parameters in the form of an array,

?free_from%5B%5D=303&free_from%5B%5D=304&free_from%5B%5D=302

I need to change this so that it matches the layered navigation query, can anyone help me on this?

I can see the action of the advanced form is returned from \Magento\CatalogSearch\Block\Advanced\Form getSearchPostUrl(), and that this then retrieved the url from Magento\Framework\View\Element\AbstractBlock getUrl($route = '', $params = []), which method should i override and how do i retrieve the params and define the structure of the query?

Any help on this would be greatly appreciated.

Best Answer

We studied such an option to allow multiple selection of filters while writing ElasticSuite.

The override should be in :

  • \Magento\CatalogSearch\Model\Layer\Filter\Attribute::_getItemsData() (URL Generation in which you should set value to the imploded value)

  • \Magento\CatalogSearch\Model\Layer\Filter\Attribute::apply (explode the value of the filter).

Then you need to fix the DI. Take cares it is located into the frontend area. You can read a full example here :

https://github.com/Smile-SA/elasticsuite/blob/master/src/module-elasticsuite-catalog/etc/frontend/di.xml

You may also encounter some SQL, SolR or ES problem to handle the array.

We have renounced to it since we do not use option ids but labels and it was a nightmare to handle multivalued this way. You can see the result here (looks likes your problem). : http://demo.magento-elastic-suite.io/index.php/women/tops-women.html?style_general%5B0%5D=Tee&style_general%5B1%5D=Pullover.

Hope it's help.