Implementing Alphabetical Search in Magento

Implementing alphabetical search in Magento can be tedious if you dont know the right approach. But its as easy as anything. You just need to alter two files to make it work.
Note: Do not edit or add this code to your core files if you wish to upgrade Magento to newer versions in the future. Please create below mentioned files in the app/core/code/local directory with complete directory structure as in Magento core.


appcodelocalMageCatalogBlockProductListToolbar.php
In this file replace the setCollection function with following:

public function setCollection($collection)
{
$this->_collection = $collection;

$this->_collection->setCurPage($this->getCurrentPage());

// we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
$postData = '';
if ($limit) {
$this->_collection->setPageSize($limit);
}
if ($this->getCurrentOrder())
{
/**********Alphabetic search Code Start From here**********/
$postData = Mage::app()->getRequest()->getParam('alpha').'%';
if(isset($postData['alpha']) && $postData['alpha']!= '' && trim($postData) !='ALL')
{
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection())->addAttributeToFilter(array(
array('attribute'=>'name', 'like'=>$postData)
));
}
else
{
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
/**********Alphabetic search Code ends here**********/
}
return $this;
}

appdesignfrontenddefaultdefaulttemplatecatalogproductlisttoolbar_bottom.phtml
Open this file an replace the code with given below:

<?php 
$search_array = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','ALL');

/*Find if the URL already contains any querystring variable or not */
if (strstr( $this->helper('core/url')->getCurrentUrl(), "&" ))
{
$separator = '&';
}
else
{
$separator = '?';
}
?>
<div>
<p class="view-mode-list-bot">
<?php
$postData = Mage::app()->getRequest()->getParam('alpha');
foreach ($search_array as $search_array_value):

/*Clean the URL*/
if (strstr( $this->helper('core/url')->getCurrentUrl(), "?" ) )
{
$new_Url = $this->str_replace_once('&','?',str_replace("?alpha=".trim($postData['alpha']),'',str_replace($separator."alpha=".trim($postData['alpha']),'',$this->helper('core/url')->getCurrentUrl())));
}
else
{
$new_Url = str_replace("?alpha=".trim($postData['alpha']),'',str_replace($separator."alpha=".trim($postData['alpha']),'',$this->helper('core/url')->getCurrentUrl()));
}

$alphaURL = $new_Url.$separator.'alpha='.$search_array_value;
?>

<a href="<?php echo $alphaURL; ?>" title="<?php echo $_label ?>" class="<?php echo strtolower($_code); ?> <?php if($search_array_value == $postData){ echo 'remove_under'; } ?>"><?php echo $search_array_value; ?></a>   

<?php endforeach; ?>

</p>

</div>

And it is done. Your alphabetical search is ready to use:)

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.

Website: http://www.webspeaks.in/

14 thoughts on “Implementing Alphabetical Search in Magento

  1. It’s throwing an error on 1.5 CE.
    “Invalid method Mage_Catalog_Block_Product_List_Toolbar::str_replace_once”

    Any ideas?

  2. Hi,

    I do not have th efile “appdesignfrontenddefaultdefaulttemplatecatalogproductlisttoolbar_bottom.phtml’
    How I can do on? I work on Magento 1.4.2

  3. wat about 1.7.00 its stating “There has been an error processing your request” on achieving this url ‘index.php/applications.html?alpha=B’ can you help me ..
    where i was wrong ?

Comments are closed.