Magento Themes and Magento Extensions

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

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

Leave a Reply

You must be logged in to post a comment.