2

On the webshop that I'm working on, the user can find products by choosing filters (attributes like color/size/category) and/or entering a search query, the search results are then displayed on the search results page. However, when the search text is left empty, Magento redirects to the homepage instead of showing results, even if other filters are filled in.

In the admin, I have set the 'Minimal Query Length' to 0, but this doesn't make any difference.

I've created a workaround by setting the search param to '0' before submitting when left empty, which will make Magento display all the products anyway. But it's not a great solution because the string '0' appears everywhere on the page including in the popular search block in the admin page.

Is there an elegant way to solve this issue without having to override all kinds of core classes?

You can find a demo of the problem here, if you replace the 'q=watch' by 'q=' I want it to show all the products.

3 Answers 3

0

I've just overridden Magento\CatalogSearch\Controller\Result\Index with a copy of that file and removed the condition if ($queryText != '') { ... }, that seems to work. But if anyone has a better solution I like to hear it.

1
  • hey @Arne i did the same as you mentions above but after doing this i didn't get any products on search page, it will be great if you help me out from this issue Sep 17, 2020 at 6:51
0

Store > Configuration > Catalog and choose Catalog. After that, expand the Catalog Search section and do the following:

Set Minimal Query Length to the minimum number of characters that can be submitted in a query. Maybe magento will let you set 0. Not 100% though.

Your solution is perfectly acceptable.

2
  • It does allow 0, but it will still reject empty search query. I assume there is no other way other than overriding the controller.
    – Arne
    Jun 17, 2019 at 18:20
  • @Arne you could create your custom controller, and forward to Result\Index only when queryText has some value. This is just an idea, it involves more work, but you wouldn't have to maintain a core controller. Jun 17, 2019 at 20:43
0

There is another way you can apply is to check the empty condition in your code and apply the below code, It mostly works and will be considered as EMPTY LIKE query which eventually you want to perform

            if(trim($searchCriteria->getQueryString()) =='')
            {
                $searchCriteria->setQueryString('%%');
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.