Implementing Alphabetical Search in Magento

app\code\local\Mage\Catalog\Block\Product\List\Toolbar.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;
}

app\design\frontend\default\default\template\catalog\product\list\toolbar_bottom.phtml
Open this file an replace the code with given below:
<?php if($this->getCollection()->getSize()): ?>

<div class=”toolbar”>

<?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):
// echo $currentUrl = $this->helper(‘core/url’)->getCurrentUrl();
/*Clean the URL*/
if (strstr($this->helper(‘core/url’)->getCurrentUrl(),”limit”))
{
$new_Url = str_replace(‘?limit=all’,”,$this->helper(‘core/url’)->getCurrentUrl());
}
else if (strstr( $this->helper(‘core/url’)->getCurrentUrl(), “?” ) )
{
$new_Url = str_replace(‘&’,’?’,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;
if($search_array_value == ‘ALL’)
{
$alphaURL = $new_Url.$separator.’limit=all’;
}
?>
<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>
<!– <a href=”#” onclick=”applyAlpha(this.title);” title=”<?php //echo $search_array_value; ?>” class=”<?php //echo strtolower($_code); ?> <?php //if($search_array_value == $postData){ echo ‘remove_under’; } ?>”><?php //echo $search_array_value; ?></a> –>
<?php endforeach; ?>
<?php //print_r($search_array_value); exit;?>
</p>
</div>

</div>
<?php endif ?>

Related posts