How To Add New Attributes To The Search Block In Prestashop

How To Add New Attributes To The Search Block In Prestashop

This post has been written as a response  to the comments on our blog.

In this article I will describe how to add a new attribute to search. The entity ‘supplier’ will be taken as an example.

image1How To Add New Attributes

One and the same product may have several different suppliers. Our customized search will be based on the name of a supplier.

image2How To Add New Attributes

In order to add certain fields to search there is no need to change the search algorithm. The fact is that a search is performed on the basis of a data table (see http://blоg.belvg.соm/develоper-tips-hоw-prestаshоp-seаrсh-wоrks.html) and all that you need is to add new values to this “seаrсh_wоrd” table.   To do this:

  • Modify the function indexation ($full = false, $id_product = false) // responsible for completing the  “seаrсh_wоrd” table
  • Create a new function getSuppliers ($db, $id_product) //receive data, required for indexation

For a successful indexation you should add more “weight” to a new parameter. This “weight” will be taken into account when ranking results. We will take the “weight” of a supplier equal to the parameter value for manufacturer name, which is stored in the field “PS_SEARCH_WEIGHT_MNAME” in the database.

And that is all. Now, when registering, the new data will be saved with reference to the product. And the standard method “Find” will be searching for data in the “seаrсh_wоrd” table without even suspecting how it appeared there J. Here you can download the ready-to-use file with the Search class.



12 comments

  1. Sara:

    1. Just replace the variables name with id_prod

    2. Replace the line:
    $product[‘sname’] = Search::getSuppliers($db, (int)$product[‘id_product’]);

    with this line:

    $product[‘id_prod’] = (int)$product[‘id_product’];

    3. You do not need the function Search::getSuppliers

  2. Hi Alex!
    Would you happen to know the names of the file where the Indexation function resides? I have added an attribute to my products (author) and would like to add that attribute to search. In order to do that, I believe it is necessary to either modify the core file which defines the indexation function or to override it. Both of which require the correct path.

  3. Actually, it is a bad practice to modify a core file, it is better to override it: i.e. to put the file into the folder

    override/classes/Search.php

    The file should inherit the core:

    The part of the code which is responsible for indexation is located in the same class Search.php.

  4. Dear Efrain, all steps for adding an attribute to the search are described in the article, so you just follow these steps and do the same way. If you have some specific problem, please, indicate it and we will try to help you.

  5. The search Id is not working for me, whaat could be wrong?

    $weight_array = array(
    ‘id_prod’ => Configuration::get(‘PS_SEARCH_WEIGHT_PNAME’),
    ‘pname’ => Configuration::get(‘PS_SEARCH_WEIGHT_PNAME’),
    ‘reference’ => Configuration::get(‘PS_SEARCH_WEIGHT_REF’),
    ‘ean13’ => Configuration::get(‘PS_SEARCH_WEIGHT_REF’),
    ‘upc’ => Configuration::get(‘PS_SEARCH_WEIGHT_REF’),
    ‘description_short’ => Configuration::get(‘PS_SEARCH_WEIGHT_SHORTDESC’),
    ‘description’ => Configuration::get(‘PS_SEARCH_WEIGHT_DESC’),
    ‘cname’ => Configuration::get(‘PS_SEARCH_WEIGHT_CNAME’),
    ‘mname’ => Configuration::get(‘PS_SEARCH_WEIGHT_MNAME’),
    ‘tags’ => Configuration::get(‘PS_SEARCH_WEIGHT_TAG’),
    ‘attributes’ => Configuration::get(‘PS_SEARCH_WEIGHT_ATTRIBUTE’),
    ‘features’ => Configuration::get(‘PS_SEARCH_WEIGHT_FEATURE’)
    );

    foreach ($products as $product)
    {
    $product[‘tags’] = Search::getTags($db, (int)$product[‘id_product’], (int)$product[‘id_lang’]);
    $product[‘attributes’] = Search::getAttributes($db, (int)$product[‘id_product’], (int)$product[‘id_lang’]);
    $product[‘features’] = Search::getFeatures($db, (int)$product[‘id_product’], (int)$product[‘id_lang’]);
    $product[‘id_prod’] = (int)$product[‘id_product’];

    I saved the search.php from the folder public_html – classes to my desktop, added the 2 lines, and saved it in public_html – override – classes.

    Then in BO i rebuild the search index.

    That should be right?

    Thanks

  6. Sara,

    have you cleared the cach of those classes in folder cache/class_index.php? Without it your override won’t work.

  7. I had somone programe it for my because it would not work.
    Afterwords I noticed that there solution made the search not work for name, and all the normal stuff..
    So today I tried downloading the seach.php from the newest release. When I upload it and rebuild the search index, everything works perfektly. Both name, ID and so on. BUT when I want to do the same for my online shop, the rebuilding process stops and shows 404.

    Do you know why this happens?? Know it seams that half the file is rebuild or something..

    // Sara

  8. Sara,

    I think you need professional help, because without seeing your code, it is difficult to help. Please contact our support team at store@belvg.com and they will help you.

  9. hi am new to prestashop i want to add product id in search but where can i find search.php file in my theme package search.tpl file is available.

  10. Midun,

    Search.php is an old controller type. It was used in version 1.6. Search.tpl is a template file.

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *