Jump to content

Filtering products considering their spec.


Recommended Posts

Hello everyone. this is my first post here in prestashop forum, so first i want to thank everybody involving and contributing to this great project which i think by far is the best eCommerce solution.


That said, i have a problem to solve and i need your advice.

This is my first time developing a prestashop theme and my client asked me to create a widget on top of the site to filter the list of products by some of their specifications.


Obviously what i need first is a html form. i feel confident enough to fetch data from database and create such a form, but what i'm not sure is where to send this form ?

Do i have to create a php file to fetch the products ?

Does products.php have support for GET or POST requests (as a built-in feature) ?

Or is there a plugin to make this work easier ?


Thanks for any help in advance.

Edited by NOjAN (see edit history)
Link to comment
Share on other sites

If at all possible I would suggest trying to use the blocklayered module.....


There are a few options to filter the list of products displayed by category.tpl (and subsequently product-list.tpl). I have implemented this for filtering on specific product features in the past (e.g. show only products with the feature "Metal" with a value "Gold".)


I created an override of the Category class and replaced the Category::getProducts() function with a custom one that filters the products based on criteria which have been POSTed to the page. This was the "easiest" way....


You could also write a module that post-processes the products for display (but it isn't very efficient). The products that will be displayed by product-list.tpl are in the {$products} smarty variable so as long as your module is run before this template file is rendered you can modify this variable and save it back to smarty. To fetch it within your module you can do:


if (isset($this->context->smarty->tpl_vars['products']))
   $products = $this->context->smarty->tpl_vars['products']->value;
   return FALSE;


After processing you would set the above smarty template var with your modified product array, although this will affect pagination (you didn't think it was going to be THAT easy). The advantage of the override is that the Category::getProducts() function can return the correct number of products in the pre-filtered list and you don't then have to worry about the detail - as far as Prestashop is concerned there are just less products in the category. The results will also be affected by the product sort options, obviously.


The actual filters themselves would be set via a form rendered in category.tpl and POSTed to the current page. If they are checkboxes in the form (from some old code of mine)....


<input type="checkbox" name="filter_feature[{$active_filter['id_feature']}]" value="{$active_filter['id_feature_value']}" onclick="submitFilter()" checked="checked" />

(Note that they are checked by default as in my case un-checking them removed them as a filter)


....you can use something similar to the following to then get an array of active filter values.

function getFilters()
 $filters  = array_filter(Tools::getValue('filter_feature', array()));
 return $filters;


Hope my rambling helps!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...