Calcetines Extreme

Calcetines Extreme
Take care of you using the best socks

Sunday, July 14, 2013

Create an Alphabetical Index on Prestashop CustomCode

Problem:
We need to add a filter in a prestashop view (.tpl file), to allow visitors to filter displayed data; i found that solution while trying to update "manufacturer" view as a customer need.

Solution:
Without buy any module we can get that functionality with a little and easy custom php code only updating 2 files:
/themes/<template>/manufacturer-list.tpl
/controllers/front/ManufacturerController.php

Lets go to see the process:
1.- Create filter criteria.
Open manufacturer-list.tpl and add HTML code for display the filter options, like that:
      A-B-C-D-E-F....
      each letter need to load the view layout adding a filter parameter, like that
      http://yoursite/index.php?controller=manufacturer&filter=M

2.- Filter Prestashop Controller Data displayed in layout
Open Controller and add that piece of code in "assignAll() function":

SearchString:
...
...
protected function assignAll()
{
if (Configuration::get('PS_DISPLAY_SUPPLIERS'))
{
$data = Manufacturer::getManufacturers(true, $this->context->language->id, true, false, false, false);
$nbProducts = count($data);
$this->pagination($nbProducts);
>>>>> HERE <<<<
foreach ($data as &$item)
$item['image'] = (!file_exists(_PS_MANU_IMG_DIR_.'/'.$item['id_manufacturer'].'-'.ImageType::getFormatedName('medium').'.jpg')) ? $this->context->language->iso_code.'-default' : $item['id_manufacturer'];

$this->context->smarty->assign(array(
...
...

Code:

$filtro = $_GET["filtro"];          //get url parameter
$indic = 0;                            
if ($filtro<>""){
foreach ($data as &$item){
if ( (substr($item['name'],0,1))<>$filtro){ //compare initial with filter option
unset($data[$indic]); //delete item
}
$indic = $indic + 1;
}
}
$data = array_values($data); //reindex array

3.- Finish
That's all, now when something click on a "letter Char" (with that example), the manufactures list will be filtered as you own need.

Hope that can help someone ;)