Advanced search in Magento and how to use it in your own way

Featured Image

It’s been a while since my last post. I’ve been working on Magento quite actively last two months. I noticed this negative trend in my blogging; more I know about Magento, the less I write about it. Some things just look so easy now, and they start to feel like something I should not write about. Anyhow…. time to share some wisdom with community :)

Our current client uses somewhat specific (don’t they all) store set. When I say specific, i don’t imply anything bad about it. One of the stand up features at this clients site is the advanced search functionality. One of the coolest features of the built in advanced search is the possibility to search based on attributes assigned to a product.

To do the search by attributes, your attributes must have that option turned on when created (or later, when editing an attribute). In our case we had a client that wanted something like

http://somestore.domain/catalogsearch/partnumber
or
http://somestore.domain/catalogsearch/brand

instead of the default one
http://somestore.domain/catalogsearch/advanced
with all of the searchable fields on form.

Some of you might say why not use the default and call it a day. Well, default one does not get very user friendly when large number of custom added searchable attributes are added in Magento admin interface. Then the frontend search form gets cluttered and users are easily to get confused.

So in our example we would like to use the advanced search and all of it’s behaviour and logic but to use it on somewhat special link and to hide unnecessary fields. Therefore, our custom  pages he would have only one input field on form and the submit button. How do we set this up? Well, all of the logic and functionality is already there.

What we need is to:

  • use the http://somestore.domain/catalogsearch/partnumber as a link
  • show only custom_partnumber field on the search form

First, we have to see where does the /advanced come from. Lets open our template folder at

app\design\frontend\default\default\template\catalogsearch\

there you will see the /advanced folder. Make a copy of that entire folder, in the same location, and name it to something like /custom.

Now your /custom folder should have 2 files: form.phtml and result.phtml.

Next in line is the /layout folder in our template. You need to open catalogsearch.xml file. Go to line 64. Do you see the <catalogsearch_advanced_index> tag there. Make the copy of it (including all of the content it hold with the closing tag also). Put the copy of that entire small chunk of code right below. Now rename all of the occurrences of “advanced” to “custom” there like on code below:

<catalogsearch_custom_index>
<!– Mage_Catalogsearch –>
<reference name=”root”>
<action method=”setTemplate”><template>page/2columns-right.phtml</template></action>
</reference>
<reference name=”head”>
<action method=”addItem”><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params/><!–<if/><condition>can_load_calendar_js</condition>–></action>
<action method=”addItem”><type>js</type><name>calendar/calendar.js</name><!–<params/><if/><condition>can_load_calendar_js</condition>–></action>
<action method=”addItem”><type>js</type><name>calendar/lang/calendar-en.js</name><!–<params/><if/><condition>can_load_calendar_js</condition>–></action>
<action method=”addItem”><type>js</type><name>calendar/calendar-setup.js</name><!–<params/><if/><condition>can_load_calendar_js</condition>–></action>
</reference>
<reference name=”content”>
<block type=”catalogsearch/custom_form” name=”catalogsearch_custom_form” template=”catalogsearch/custom/form.phtml”/>
</reference>
</catalogsearch_custom_index>

Do the same for <catalogsearch_advanced_result> tag.

Now go to the app\code\core\Mage\CatalogSearch\Block folder. And make a copy of /Advanced folder naming it /Custom. Open /Custom/Form.php and replace class name Mage_CatalogSearch_Block_Advanced_Form with Mage_CatalogSearch_Block_Custom_Form, then open /Custom/Result.php and replace class name Mage_CatalogSearch_Block_Advanced_Result with Mage_CatalogSearch_Block_Custom_Result.

Inside Form.php there is getModel() function. DO NOT replace the Mage::getSingleton(‘catalogsearch/advanced’); with Mage::getSingleton(‘catalogsearch/custom’);. The point is to use the default advanced search logic here. Same goes for getSearchModel() function inside Result.php file.

Next in line, controllers. We need to make the copy of AdvancedController.php and name it CustomController.php, then open it and replace the class name Mage_CatalogSearch_AdvancedController with Mage_CatalogSearch_CustomController.

Inside this CustomController.php there is a function called resultAction(). You need to replace the ( … Mage::getSingleton(‘catalogsearch/advanced’) … ) string ‘catalogsearch/advanced’ with ‘catalogsearch/custom’. This is the one telling the browser what page to open.

Now if you open your url link in browser with /index.php/catalogsearch/custom instead of /index.php/catalogsearch/advanced you will see the same form there.

And for the final task… As we said at the start, the point of all this is to 1) get more custom link in url and 2) display only one custom searchable attribute field in form. Therefore, for the last step we need to go to the template\catalogsearch\custom folder and open form.phtml file.

Inside that file, there is one foreach loop that goes like
<?php foreach ($this->getSearchableAttributes() as $_attribute): ?>

All we need to do now is to place one if() condition right below it. If your custom attribute name (code) is “my_custom_attribute” then your if() condition might look something like

<?php foreach ($this->getSearchableAttributes() as $_attribute): ?>
<?php if($_code == ‘my_custom_attribute’): ?>

<?php endif; ?>
<?php endforeach; ?>

And you are done. Now you have somewhat more custom url, and only one custom field displayed on that url.

This might not be the best method of reusing already written code, however I hope it’s any eye opener to somewhat more elegant aproach.

Enyoj.

50
Top

Care to rate this post?

Author

Branko Ajzele

Ex Inchooer

Worked at Inchoo as a Backend Developer/CTO from 2008 to 2013

Other posts from this author

Discussion 50 Comments

Add Comment
  1. Mathew

    That is a really useful article. Not only is advanced quicker it can also be customised as you have done so – that was too tough for me when I tried it!
    The holy grail of search is to have a category breakdown, e.g. x results in a, y results in b etc. This is how proper stores have it.

  2. Mathew

    Cheers dude!

    I managed to get this working on my second attempt, seemed I made a mishtake on the layout.xml file! All is working nicely, many thanks for the tutorial!

  3. Mathew

    Oh no I didn’t get it right! Any help with this? I followed the above with the word brands for my custom search. I now get an error when opening a search:

    Warning: include(Mage/CatalogSearch/Model/Brands.php) [function.include]: failed to open stream: No such file or directory in /var/www/html/app/code/core/Mage/Core/functions.php on line 73

    I tried to bodge it and scarper by adding in a Brands.php file based on the Advanced.php but that just returned the full catalog!

    I double checked everything, any ideas why it is trying to call a Brands.php file instead of Advanced.php?

  4. Does anyone know of any type of Magento Admin Panel Training course?

    I have had Magento for a few weeks now and I know there are tons of functions and tools available to me that I just do not know how use (or at least not how to use correctly) Anyone have any info regarding a training course?

    Peace
    Matthew

  5. Hello…

    I’ve gotten everything to work, sort of…. when I enter a search it returns ALL the products within the catalog, instead of any search.

    Any suggestions?

  6. Diego

    Hi! Nice tutorial, but I got one question: I got 36 simple products (all with several attributes), whose visibility is “Search”-only. All simple products are grouped together to one product. If I use advanced search based on an attribute of the simple products I get results for the simple products, but not the grouped product, where the simple products belong to. And so the Simple Products whose visibility is on “Search” are visible and clickable, which should not be possible. Instead the grouped product should come up as the search result. Is this do-able???

  7. Hello,

    Nice tutorial.
    Could you help me with something?
    I’m a magento newbiw, and I’m build a custom skin, I have almost all the pages done, my problem is that I can’t manage to customize the advanced search page, it”s not following the same design, I can’t manage to find out why.

    THanks

  8. @DoruDM

    Not sure what exactly the issue is? Have you turned off all caching, turned on path hints? This should point you to the right file when you refresh the frontend. Then with a little help of FireBug you should be able to fully see and customize the styling stuff.

  9. I’ve implemented this and it works fine however I would like to move the custom search from app\code\core\Mage\CatalogSearch to app\code\local\MyCompany\CatalogSearch.

    Any ideas how to do this?

    Many Thanks.

    Chris.

  10. @Christopher Shennan

    Well, one way to do it is to create a module, add new layout xml file to it, map to some url… Not sure what the final result needs to be. Another way is to simply override the default /core CatalogSearch.

  11. p.s. This is now relatively “old” article. I myself would use entirely new approach now, one that uses module.

  12. Some people reported that the custom search return everything. I guess in the article, the paragraph is not right:

    Inside this CustomController.php there is a function called resultAction(). You need to replace the ( … Mage::getSingleton(’catalogsearch/advanced’) … ) string ‘catalogsearch/advanced’ with ‘catalogsearch/custom’. This is the one telling the browser what page to open.

    Should not change that line at all. That’s how I get mine works. ( initially following that direction, but get everything back).

  13. Sohit

    Thanks a lot, this stuff is really a life savior.

  14. Abdul Ghafoor

    Hi,
    I want to add an attribute in advance search but having different input type. Suppose I’ve an attribute named as length. I’ve added this attribute by choosing input type dropdown in admin panel. But in advance search I want this attribute work as min/max range as text fields just like price. I know I can change the input type from dropdown to text field and it works but I can’t change it.

    Can some one help me out here. Thanks in advance.

  15. Jennifer

    I get an error when clicking the search button on my custom form that file cannot be found: Mage/CatalogSearch/Model/Custom.php

    Any ideas? Thanks!

    Jen

  16. Steve

    Hi, when I hit search button magento gives me this error:
    File is writable by others, please fix permissions and try again.

    Help me please!!!!!!!

  17. Afzaal Ameer

    Can someone help me out i want to display all those fields which are set to be searchable in quick search.

  18. nm

    I followed all the steps u told above…..If I open this url link in browser /index.php/catalogsearch/advanced …..I see the form but …:( /index.php/catalogsearch/custom ..I dont see any form ???Can u help me out

  19. nm

    Gooooooood :) Now its working….just I had to do one more step for search……I had to make a copy of advanced.php in app/code/core/mage/catalogsearch/model / and make its name…..custom.php and changed its class name…..used custom instead of advanced :) Now search is being done in good way

  20. nm

    May u guide me…if I want to make more custom search pages….I repeated the same with searchbysize name….but its not working now…for the second search in same project …..search page is not being opened

  21. vishal lakhani

    hi i get this error can any 1 tell me why this error comes when searching for product

    Fatal error: Call to a member function addFilters() on a non-object in /home/gizmoheart/public_html/app/code/core/Mage/CatalogSearch/controllers/CustomController.php on line 48

  22. vishal lakhani

    i want to create new quick search as here show how create new advanced search can any 1 help me?

  23. when i use firebug to check form then i getting action form is “……/index.php/cms/index/result/”,and when submit then it not correct url,site say 404 page not found.
    plz tell me what im not correct when do follow above tutor ?

  24. Dennis

    Hi

    Is this code a way to make a click link in attributes like amazon.com have on Authors

  25. John Tucker

    Hi

    Nice Article. thanks for it.

    I want to know how to rewrite search url

    http://www.MagentoDomain.com/catalogsearch/advanced/result/?name=htc+diamond

    to
    http://www.MagentoDomain.com/name/htc-diamond

    Your help is appreciated.

  26. hello

    i am having problem with quick search , my quick search lists all the products whether they are visible for search or notand whatever i search it lists the whole store ..

    if you have an idea please let me know
    THANK YOU

  27. NCG

    does this works on magento ver 1.5.0.1? how can i install this the latest version and where can i download the extension? any help PLEASE

  28. rakesh

    Call to a member function addFilters() on a non-object error please help me

  29. Jonathan

    Wow, this blog seems to be a treasure for magento beginner like me :D thank you for awesome job guys!

    Branko Ajzele
    19-05-2009 at 17:16 | #

    p.s. This is now relatively “old” article. I myself would use entirely new approach now, one that uses module.

    Hi Branko

    Is there any chance to provide a updated tutorial on this topic please, if Im not asking for too much :P

    Me and I guess many other readers of this blog would appreciate it a lot.

    What are the chances for an updated tutorial? : ))

  30. Jonathan

    Hi

    I tried this tutorial on my magento 1.4.1.1 and managed to create a custom advanced search and it looks like everything is working.

    Problem is that I cant show ONLY the attributes I want, all the attributes that are set “Yes” to show in advanced search are given in my custom search too.

    My attribute name is for_brand, so I tried
    getSearchableAttributes() as $_attribute): ?>

    getAttributeCode() ?>

    and also ended php if, right before the php foreach, but all I get on my custom search page now is empty page, only header “Search Settings” and the Search Button are shown but the search form itself is gone : ((

    Can you help me with this please?

    Regards
    John

    P.S. and if I want to show more than one attribute, how should I do that? what code shall i put there for my attributes?

    At the moment I want to show two attributes, “for_brand” and “for_model”

    Thanks a lot in advance…

  31. Jonathan

    It did cut off my code snippet, so I’ll try it again without php opening & closing tags:

    foreach ($this->getSearchableAttributes() as $_attribute):

    if($_code == ‘custom’):

    $_code = $_attribute->getAttributeCode()

  32. hardik

    Its really nice, but how can i display layered navigation on that page. I have put below code in catalogsearcg.xml file;

    Its give me sql error.

    Please any one have solution for this problem.

  33. vishal

    Hi @nm and all I have tried this in magento 1.5.0.1 But i also get Blank form.. I have followed all instruction But still getting blank in middle portion coming blank instead of advanced FORM and checked by Making Template Path Block is not calling even i created Custom.php in model as nm said.. but still its blank so @nm or any one one help me Please…

  34. vishal

    Hi All resolved Some Issue with XML part i have copied from here then its was not working then copy from my theme’s catalaogsearch.xml copy portion then paste and then its works… hope help to some 1 thanx to all

  35. Abul

    Hi,

    Can we save the results or search keywords at the user end?

  36. How can i create custom search module not the advanced search module?

  37. Yash

    John Tucker, were you able to rewrite the Search URL?

    This does not seem possible.

  38. Nitesh

    hello,
    it gives me this king of error when i check the url

    There has been an error processing your request.

    Exception printing is disabled by default for security reasons.

    Error log record number: 1681139917

    how to resolve it plz, quick reply

  39. Kunal

    Followed all teh steps above but when i do a custom search getting this error:
    Fatal error: Call to a member function addFilters() on a non-object in /app/code/core/Mage/CatalogSearch/controllers/CustomController.php on line 48 Fatal error: Class ‘Mage’ not found in /app/code/core/Mage/Core/functions.php on line 244 Fatal error: Class ‘Mage’ not found in /app/code/core/Mage/Core/Model/Mysql4/Session.php on line 88

    can you pls help, what am i doing wrong?

  40. agam

    It’s really nice and help full but i got an error msg when i run this.

    i have created all folder and changed class name advanced to custom . after this it showing form on custom url but when a search any value it gives error:

    Fatal error: Call to a member function addFilters() on a non-object in /app/code/core/Mage/CatalogSearch/controllers/CustomController.php on line 48

    Mage::getSingleton(‘catalogsearch/custom’)->addFilters($this->getRequest()->getQuery());

    please help

  41. Tom

    Yeah I get the same thing, I’m thinking this might be a bit too out of date now. May be wrong but it doesn’t seem to be working anymore. Nice tutorial though.

  42. Chris

    Hi Guys,

    I did this b4 on a 1.4 magento version but when I tried doing this on a 1.6 It doesn’t seem to work,

    I’m not sure why is there something different with how the 1.6 is using xml?

  43. Sonal

    Really nice, but the only thing did not work for me is :

    “Inside this CustomController.php there is a function called resultAction(). You need to replace the ( … Mage::getSingleton(‘catalogsearch/advanced’) … ) string ‘catalogsearch/advanced’ with ‘catalogsearch/custom’. This is the one telling the browser what page to open.”

    Mage::getSingleton(‘catalogsearch/advanced’) is correct no need to update it to “custom”

    Thanks for the post.

  44. rishu

    how do i pass multiple attribute codes in advance search url as parameters please help

  45. Anil Kasar

    hi,
    I have there drop down list in want to search navigate and product after last drop down item select. drop down are dependent each other (i mean third depends on second, second depends on first) third drop down are attributes values of the second drop down category item.
    it should be work like filters. and i have added this drop down in header. please help me out.
    Thanks in Advance

  46. Aru

    i am working on magento 1.7.I have installed new theme but, they had removed advanced search.So i tried adding advanced search using this tutorial.i followed all the steps given.But when i enter /index.php/catalogsearch/advanced and /index.php/catalogsearch/custom in the url its not showing up any form..how to go about with it

  47. Thanks, your blog is very useful :D

  48. carl

    Hi, Excellent tutorial, just what I was looking for, How ever Have an issue, search page displays correct and posts to results page. But results page is blank?

    /productfinder/result/?name=&wattage=&lumen=&cap=&cct=&dimmable=6&beam=&no_led=&lum_w=&cri=&power_factor=&ip=&measurement_mm=&cutout_mm=&voltage=&output_voltage=&output_current_ma=&input_current_amps=&equi_halogen_watt=&product_range=&rohs=

    No errors or 301 error so page is their it just blank?

    I went through the tutorial twice, and still not working?
    Any ideas?
    Thanks

  49. carl

    Hi People relating to above.. I found the error on magento 1.8 and now working it seems.

    ON CustomController DO NOT change the result/Action leave as ‘catalogsearch/advanced’
    and seems to work for me

    Thanks

  50. CJ

    Carl, are you reporting that the original instructions in the Advanced Search tutorial work in 1.8?

    Thanks for updating the thread.

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top