Question

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.

Was it helpful?

Solution 3

I solved it!!

This was actually meant to be handled at javascript but not php as I was thinking.

The query was being generated by the form. I was not aware of the behavior that the form will pass the inside field attributes i.e value and name as query to the submit action url.

So, for example, if I have a simple form as follows:

<form id="some-form" action="http://www.google.com">
     <input id="category-search" style="display:none;" type="text" name="cat" value="Mobile" />
     <input type="text" name="q" value="Blackberry"/>
</form>

The above form will create the following URL on submit action:

http://www.google.com/?cat=Mobile&q=Blackberry

In default magento, only one input field is present in the search form

So, what I did is added a keypress event which runs whenever the user presses colon (:) and then splitted the value in search text to Category Name and Product Name. Like this:

Mobile:Blackberry
=>   CategoryName = "Mobile";
     ProductName  = "Blackberry";

I am just sharing the logic here.. I hope it will be helpful for someone.

Hint: I kept the #category-search as a select element, instead of input element as I shown above, this will have all the category and sub-category names. Like this:

<select id="category-search" style="display: none;">
    <option data-id="37">Mobile</option>  <!-- category Id and category name -->
    <!-- Many options here -->
</select>

OTHER TIPS

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? :-)

You cant simple change the url used, as this would need a redirect (what i dont suggest here)

But you can go into the controller_action_predispatch event and manipulate the request object.

for the event: http://www.nicksays.co.uk/magento-events-cheat-sheet-1-7/
there should be an event, which is only triggered for the catalogsearch.

for the request object: http://phprelated.myworks.ro/set-or-get-request-variables-in-magento/
but reading a bit into the search sure is better

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top