Sunday, July 14, 2013

Create an Alphabetical Index on Prestashop CustomCode

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.

Without buy any module we can get that functionality with a little and easy custom php code only updating 2 files:

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:
      each letter need to load the view layout adding a filter parameter, like that

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

protected function assignAll()
if (Configuration::get('PS_DISPLAY_SUPPLIERS'))
$data = Manufacturer::getManufacturers(true, $this->context->language->id, true, false, false, false);
$nbProducts = count($data);
>>>>> 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'];



$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 ;)


  1. The blog posted is very interesting from all aspects and it will surely benefit the readers by all means.
    Prestashop Developer