0

Magento 2.4

Is there a way to add an attribute to the search query every time a search is triggered?

Example searching for "test": https://www.example.com/catalogsearch/result/?q=test

I want to add status=41%2C42%2C43%2C45, so that it looks like this in the end:

https://www.example.com/catalogsearch/result/?q=test&status=41%2C42%2C43%2C45

I thought this might not be too hard to do, but I found nothing useful so far. Is it possible, also considering that ther search is done with elasticsearch?

Thanks!

1

2 Answers 2

1

If you want to add the status to filter automatically, I have an introduce for you in here https://github.com/hieuhidro/customfilter

  • This module adding a custom filter which allow you modify the layer navigator and product collection before push the query to elasticsearch or mysql search.
4
  • Thanks! I installed the extension, how do I use it? There seems to be no entry for it in Magento admin? Dec 5, 2022 at 12:41
  • That is an example module, you have to follow to introduce how to adding a custom filter.
    – HoangHieu
    Dec 6, 2022 at 0:51
  • I modified the collection according to my needs and it works just fine, THANKS! Dec 6, 2022 at 8:18
  • You are welcome.
    – HoangHieu
    Dec 6, 2022 at 9:31
1

Yes it's always possible. THe only thing you have to do is to modify the base template to add the status tag in the generaated url by concatenation it should work.The it would be sent like any other get param that you can retrieve.

If the final url isn't called in a template but in an extra elastic block or controller file, the same logic applies. All you have to do is to find where the link is called.

Assuming your url is built in there and assuming you want to do it for every request

here is the method you need to extend from vendor/magento/module-search/Helper/Data.php

/**
 * Retrieve result page url and set "secure" param to avoid confirm
 * message when we submit form from secure page to unsecure
 *
 * @param   string $query
 * @return  string
 */
public function getResultUrl($query = null)
{
    return $this->_getUrl(
        'catalogsearch/result',
        ['_query' => [QueryFactory::QUERY_VAR_NAME => $query], '_secure' => $this->_request->isSecure()]
    );
}

All you have to do is to make a preference on that class, and to rewrite the method concatenating on the return ."&status=".$status

Second argument of getUrl is also taking params, may be also check if you cannot set the status directly in there rather than concatenating which is kinda ugly way to do it.

6
  • Thanks for your answer ... could you give me some more details about where to place what? Some code maybe? Thanks! Nov 28, 2022 at 9:42
  • Not really without knowing what you have in mind. What is the status you want to send ? How is it sent ? Which page or url is it coming from ? If it's generated dynamically on the webpage you need to edit the template of the search. If it's not dynamic may be you can just extend the search controller form elastic and get your data there without even any extra parameter...too many unknown on what you what to achieve so far^^
    – Claims
    Nov 28, 2022 at 9:57
  • Well, like I said, I want to attach the status attribute to every search query ... thats what I have in mind :-). Nov 28, 2022 at 10:02
  • 1
    you will find an edit.
    – Claims
    Nov 28, 2022 at 10:34
  • I tried, but it doesnt work. It adds the parameter to the query in the form, but once I search, it only searches for the entered search term. Ideas? Dec 5, 2022 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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