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

16 Comments 8th NOV 2008 | Posted by Branko Ajzele in Magento

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

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.

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • FriendFeed
  • NewsVine
  • Sphinn
  • Tumblr

There are 16 comments (Add Yours +)

  • [...] is a new article titled: Advanced search in Magento and how to use it in your own way. This one is a direct result [...]

  • 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.

  • 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!

  • 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?

  • 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

  • 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?

  • 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???

  • 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

  • @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.

  • 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.

  • @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.

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

  • 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).

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

  • Abdul Ghafoor Says

    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.

  • 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

Leave a Comment

Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009