nana.chorage
nana.chorage

Reputation: 506

Alphabetical Product list in magento

I am going to show alphabet sequence at the footer ..like

A | B | C | ....Y | Z 

if user clicks on any alphabet , say user click on letter "B" .. product listing page should come with Products name starting with letter "B"

any extension available in magento or I have to code for this .

Upvotes: 0

Views: 2350

Answers (2)

user3122108
user3122108

Reputation: 21

I Just Modified The Above Code ....... Which is Working Perfectly..

Go To Your Admin Panel "app/design/frontend/default/custom_theme/layout" add following Block To Your "Catalog.xml".

<block type="catalog/product_list" name="alphabetical_search" template="catalog/product/list/alphabetical_search.phtml"/>

Right Under

<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">

Create New Html Block with name "alphabetical_search.phtml" Inside app/design/frontend/default/custom_theme/template/catalog/product/list

Add Following Code In File

<?php
//Create Array For Alphabets A-Z
$search = 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' );
?>
<div>
<?php 
//Get Value Of Search String From URL
$postdata = Mage::app()->getRequest()->getParam('alpha');
foreach( $search as $value )
{
//If Current Url Already Using Filter Then Clean It...
if (strstr( $this->helper('core/url')->getCurrentUrl(), "?" )) 
{
//Clean Url Each Time For New Filter
$newurl = substr( $this->helper('core/url')->getCurrentUrl(), 0, strpos( $this->helper('core/url')->getCurrentUrl(), '?' ) );
}
// Create Mainurl For search...
$mainurl = $newurl.'?alpha='.strtolower($value);
?>
<a href="<?php echo $mainurl; ?>">
<?php   if( $this->helper('core/url')->getCurrentUrl() == $mainurl )
{
?>
<strong><?php echo $value; ?></strong>
<?php
}
else
{
echo $value;
}
?>
</a>
<?php
}
?>
</div>

Go To app/code/core/Mage/Catalog/Block/Product/List

Find This Function "public function setCollection($collection)"

Replace Following Code With

if ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}

With

if ($this->getCurrentOrder()) {
// Get String Name To Filter
$postdata = Mage::app()->getRequest()->getParam('alpha');
// if postdata is not empty and not equal to all then ....
if( $postdata != '' && $postdata != 'all' )
{
// Custom Set Attribute To Filter Using Name 
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection())->addAttributeToFilter(array(array('attribute'=>'name', 'like'=>$postdata.'%')));
}
else
{
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
}

Inside "app/design/frontend/default/responsive/template/catalog/product/list" Open File "toolbar.phtml" and paste Following Code any where you want

<div class="alphabetical_search">
<?php echo $this->getChildHtml('alphabetical_search'); ?>
</div>

Add That's It !!!!

Upvotes: 2

Dushyant Joshi
Dushyant Joshi

Reputation: 3702

Please create below mentioned files in the app/core/code/local directory with complete directory structure as in Magento core.

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;
    }

And

app\design\frontend\default\default\template\catalog\product\list\toolbar_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>

Now add the function 'str_replace_once' in the following file

app\code\local\Mage\Catalog\Block\Product\List\Toolbar.php

The function,

public function str_replace_once ($needle, $replace, $haystack) {
     // Looks for the first occurence of $needle in $haystack
     // And replaces it with $ replace.
         $pos = strpos ($haystack, $needle);
         if ($pos === false) {
             // Nothing found
         return $haystack;
         }
    return substr_replace ($haystack, $replace, $pos, strlen($needle));
 }

And it is done. Your alphabetical search is ready to use:) To add it to footer you can add the following block into the footer bloack.

<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
                            <block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>

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.

Upvotes: 0

Related Questions