Tuesday, June 4, 2013

Catalog Search not returning expected results in Magento

Many customers complain about Magento's default catalog search results. Here is the solution to get better results from Magento Catalog Search.

1. Copy app/code/core/Mage/CatalogSearch/Model/Resource/Fulltext.php to app/code/local/Mage/CatalogSearch/Model/Resource/ folder and open file.

2. Navigate to function prepareResult() and find below set of code:

$words = Mage::helper('core/string')->splitWords($queryText, true, $query->getMaxQueryWords());
foreach ($words as $word) {
$like[] = $helper->getCILike('s.data_index', $word, array('position' => 'any'));

and replace it with:

$like[] = '`s`.`data_index` LIKE :likew';
$bind[':likew'] = '%' . $queryText . '%';

Next replace

$likeCond = '(' . join(' OR ', $like) . ')';


$likeCond = '(' . join(' AND ', $like) . ')';

3. Login to Admin Panel of your magento. Navigate to System --> Configuration.

4. Now click on  Catalog --> Catalog tab on left. In Catalog Search panel, set Search Type to like.

5. Just reindex all data and your store's search should be working fine now.
This process was tested on magento version 1.7.0 and  above.

Saturday, February 16, 2013

Magento: Redirect to login page if user not login

Here is simple magento code to redirect to login page, if user is not logged in:

    $redirect_url = Mage::getUrl('customer/account/login/');
    $current_url = Mage::helper('core/url')->getCurrentUrl();
    if((!$this->helper('customer')->isLoggedIn()) && ($current_url != $redirect_url)){

Use this code and enjoy.. :)