2

I need to add an exception to Magento's search to allow slashes / instead of escaping them to %252F - both in the search field and URL.

For example, a search for 'LIF-100/CC' currently gets converted to 'LIF-100%2FCC' (URL: /catalogsearch/result?q=LIF-100%252FCC), modifying the URL manually to '/catalogsearch/result?q=LIF-100/cc' gives the desired results.

I've got as far as the function getQueryText and am trying to figure out whether the following is what I'm after for this...

$this->_queryText = is_array($this->_queryText) ? ''
    : $stringHelper->cleanString(trim($this->_queryText));

I've tried various ways of removing the clean string / trimming from the above with no success (search query is always the same) so am wondering whether I should be looking at the function prepareResult instead?

Or should I be in fact searching for where the generation of the URL is formed?

1 Answer 1

1

Okay, numerous hours later I've almost got a solution to this.

Edit: Now have got a solution, make sure you read the updates at the bottom of answer.

Probably will prove very useful to any stores that require SKU codes to be searchable or have a number of products that contain special characters because now at last, by implementing the below, Magento's search will return results for these search queries.

After some debugging, I identified that the URL's were being encoded (twice) in Url.php located at /app/code/core/Mage/Core/Model.

$query = http_build_query($params, '', $escape ? '&' : '&');

As pointed out in function getSearchUrl in /app/code/core/Mage/CatalogSearch/Block/Term.php.

public function getSearchUrl($obj)
{
    $url = Mage::getModel('core/url');
    /*
    * url encoding will be done in Url.php http_build_query
    * so no need to explicitly called urlencode for the text
    */
    $url->setQueryParam('q', $obj->getName());
    return $url->getUrl('catalogsearch/result');
}

So at this point, I knew I needed to reverse urlencode and output this result instead of the escaped query for search.

I was able to reverse this process using the following:-

$noescape = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($this->_queryText));

Adding this to function getQueryText in /app/code/core/Mage/CatalogSearch/Helper/Data.php then allowed me to return the non escaped query text with $noescape so I simply add a new function in Data.php underneath getEscapedQueryText like so:-

/**
 * Retrieve search query without escaping special characters
 *
 * @return string
 */
public function getNoEscapedQueryText()
{
    return $noescape;
}

Now all I need to do is figure what code is outputting the query in the URL so I can modify this to use $noescape but struggling to find this at present. Will update my answer as soon as I find it.

Update

I know that return $this->_queryText; is what is outputting the search query in the URL now, I just haven't figured out how to incorporate the above into this output (I haven't managed to figure out the correct syntax).

Currently fiddling about with variations of

return $this->preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($this->_queryText));

and

return $noescape($this->_queryText);

Update 2

$noescape above can now be ignored as we can just modify return $this->_queryText; directly with:-

return $this->_queryText = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($this->_queryText));

End result is that we can now use special characters in Magento's search field that returns results and doesn't escape them to %252F2 for example. The URL's will still contain the likes of %252F2 but it retains the special characters in the search field via getQueryText so it still resolves the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.