1

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.

3

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

| improve this answer | |
  • Hi, I want to do this explode just before the page is rendered. I mean by default if I search anything that will be added to param q but I want to even add it to cat param. please help. If I got this then I can handle string manipulations as you explained above easily. and also I am not sure whether I should do this in javascript(client-side) or php. – Mr_Green Dec 25 '13 at 11:23
  • Sorry, I don't understand what you want to achieve... – Fabian Blechschmidt Dec 25 '13 at 16:34
  • Fabian, I solved it and added as answer below.. thanks for your support. – Mr_Green Dec 27 '13 at 6:40
0

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

| improve this answer | |
0

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>
| improve this answer | |
  • You should think about having a select box for the category maybe, for better useability – Fabian Blechschmidt Dec 27 '13 at 8:02
  • Yeah I did kept a select box for category :).. I even explained the same in hint.. I think I am not clear in explaining. – Mr_Green Dec 27 '13 at 8:17
  • Oh no, I'm sorry, I think I didn't to the end :-( – Fabian Blechschmidt Dec 27 '13 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.