Advanced search in Magento and how to use it in your own way
Posted by Branko Ajzele under Magento @ 8th NOV 2008

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.
5 Responses to “Advanced search in Magento and how to use it in your own way”
Leave a Reply
Recent Posts:
- 02 Jan Contact Form in Magento
- 31 Dec Review of Zend Studio for...
- 28 Dec What is a #pragma mark?
- 22 Dec More Flexible Approach fo...
- 19 Dec Is YouTube the Future of ...
- 17 Dec CRU(EL)D: Reinventing the...
- 17 Dec Toni Anicic on Vidi Web T...
- 16 Dec 1 PSD and 2 Web Tools for...
- 12 Dec Wordpress 2.7 Is Out—an...
- 08 Dec First time working with O...
Tags:
color iphone tutorials tool products Online Marketing object marketing advertising management method seo product Google development class view project jquery Wordpress template Magento search Tools ajax framework oscommerce theme navigation graph
Recent Comments:
- Tomislav Bilic on CRU(EL)D: Reinventing the Wheel:
Hi Anshul, I guess you were speaking about this project: http://propel.phpdb.org/trac/... - Anshul on CRU(EL)D: Reinventing the Wheel:
May be using a Class generator which will generate Object class based on your DB schema can be of he... - Adam on Review of Zend Studio for Eclipse Professional Edition 6.1:
I used to use Zend Studio exclusively for all my development work. But I can't justify the price tha... - Hisham on Featured products on Magento frontpage:
Does this tutorial works with 1.2 ?... - Suomedia on What is the best shopping cart?:
Which is best really depends on the needs of the user. osCommerce has and continues to serve many us... - edulink.cz on Manual creation of Google Sitemap in Magento:
Thank you, very usefull!... - Branko Ajzele on Review of Zend Studio for Eclipse Professional Edition 6.1:
Hi Tony. Nice to hear you like the article. I still havent manage to play with Magento in it. Will... - Reddy on UPS API Quest:
Hello Ben, I need UPS API docs. I did a lot Google searches, but didn't find anywhere. So please ... - Tony on Review of Zend Studio for Eclipse Professional Edition 6.1:
Great Article! I always look forward to anything that will help me with Magento. I have been using C... - New Revolution Theme Released on Revolution 2 is launched:
Brian just launched a new theme called Streamline just the other day. Merry Christmas, it's free to...

[...] 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