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.
One and the same product may have several different suppliers. Our customized search will be based on the name of a supplier.
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public static function getSuppliers($db, $id_product) { $suppliers = ''; $suppliersArray = $db->executeS(' SELECT s.name FROM '._DB_PREFIX_ . 'product_supplier sp LEFT JOIN '._DB_PREFIX_ . 'supplier s ON (s.id_supplier = sp.id_supplier) WHERE sp.id_product = ' . (int)$id_product . ' GROUP BY s.id_supplier' ); foreach ($suppliersArray as $supplier) { $suppliers .= $supplier['name'] . ' '; } return $suppliers; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$weight_array = array( 'sname' => Configuration::get('PS_SEARCH_WEIGHT_MNAME'), //indicate priority in results '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') ); Call the function getSuppliers within the function indexation(): … while (($products = Search::getProductsToIndex($total_languages, $id_product, 50)) && (count($products) > 0)) { // Now each non-indexed product is processed one by one, langage by langage 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']); //get supplier details $product['sname'] = Search::getSuppliers($db, (int)$product['id_product']); … |
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.
PrestaShop Support & Maintenance
Take your online store to the next level with BelVG PrestaShop Support & Maintenance
Visit the page
Midun,
Search.php is an old controller type. It was used in version 1.6. Search.tpl is a template file.
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.
Sara,
I think you need professional help, because without seeing your code, it is difficult to help. Please contact our support team at [email protected] and they will help you.
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
Sara,
have you cleared the cach of those classes in folder cache/class_index.php? Without it your override won’t work.
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
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.
how do i add supplier_reference to the search?
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.
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.
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
If I would like Product ID to be In the search. How do I do that??