Dali
Dali

Reputation: 7882

Magento - sort/order the suggestions in the Autocomplete search form

how can I change the sort/order in the suggestions of the Autocomplete search form.

I use Magento version 1.4.1.1

Thanks for help.

Upvotes: 1

Views: 1693

Answers (1)

Tim Bezhashvyly
Tim Bezhashvyly

Reputation: 9100

To make it quick, copy /app/code/core/Mage/CatalogSearch/Model/Mysql4/Query/Collection.php into /app/code/local/Mage/CatalogSearch/Model/Mysql4/Query/Collection.php in local scope file find this line:

->order('popularity desc');

and replace it with this:

->order('query_text asc');

This is enough to make it work.

If you are interested in how does it work here you go.

  1. form.mini.phtml sends request to suggestAction function of Mage_CatalogSearch_AjaxController though the following url: http://www.yourdomain.com/catalogsearch/ajax/suggest?q=query (may be quite useful for debugging).
  2. Then instance of Mage_CatalogSearch_Block_Autocomplete makes a query to database catalogsearch_query table through getSuggestCollection function call.
  3. Inside this function the actual collection is built but for some reasons I was not able to apply to setOrder function to it so I had to dug one step deeper.
  4. The clue lies in setQueryFilter function from the file you just copied above. Replacing the ordering method did the trick.

Hope it helped.

Upvotes: 3

Related Questions