Magento 1.8 – String Manipulations on Catalogsearch Result URL’s Query

advanced-searchcatalogsearchmagento-1.8PHPsearch

I am trying to include "Category" search in the Advanced search of my project. By going through some Stackoverflow questions, I realized that magento has built-in code for category search but they haven't used it.

I mean while searching if I add a param as cat then that will represent Category. So, for example:

www.mywebsite.com/catalogsearch/result/?cat=37&q=shoe

In the above url, the cat param represents the id of Category. Hence, the search of string shoe will be specific to the provided category id.

(ignore me if this is a known thing and a unnecessary explanation)

Now, what I am trying to do is split the search text in to two halves based on a colon(:). such that the first part will be the "Category Name" and the second half will be the "Product Name" then I will append the catalogsearch/result url this new params.

For example, if the search string provided by user is Mobile:Blackberry, then I will extract the category Id from the provided category name and pass the query as follows:

?cat=37&q=Blackberry

I am not sure where exactly I should do this string manipulations.

Please guide me.

Best Answer

I would try with explode:

list($categoryName, $productName) = explode(':', Mage::app()->getRequest()->getParam('q'));

And when you set this up in the predispatch event, you can just put your category back into $_REQUEST or $_POST, just check the code, where it gets the cat from.

I didn't find any code which reads the cat param, but I think you have tried it? :-)

Related Topic