Jump to content

[FIX] Include search by attribute references in search results


genix
 Share

Recommended Posts

Hello,

 

I encountered a problem, when I tried to associate each product attribute with an unique reference. When I tried to search for this reference, the product wouldn't show up in the search result.

 

So I looked a bit into the prestashop search code, and could fix this problem with some simple modifications in "classes/Search.php".

 

How to fix:

Search for "public static function getAttributes" in "classes/Search.php" and replace the whole function with the following code:

 

public static function getAttributes($db, $id_product, $id_lang)
{
 $attributes = '';
 $attributesArray = $db->ExecuteS('
 SELECT al.name, pa.reference FROM '._DB_PREFIX_.'product_attribute pa
 INNER JOIN '._DB_PREFIX_.'product_attribute_combination pac ON pa.id_product_attribute = pac.id_product_attribute
 INNER JOIN '._DB_PREFIX_.'attribute_lang al ON (pac.id_attribute = al.id_attribute AND al.id_lang = '.(int)$id_lang.')
 WHERE pa.id_product = '.(int)$id_product);
 foreach ($attributesArray AS $attribute)
  $attributes .= $attribute['name'].' '.($attribute['reference'] != "" ? $attribute['reference'].' ' : '');
 return $attributes;
}

 

Now just reindex the products in the search configuration panel.

 

Regards, genix.

  • Like 4

Share this post


Link to post
Share on other sites

I'm using 1.4.7.3 and I didn't even have

function getAttributes

This made a fantastic difference. I can now search for red tops and only tops that are available in red show in the results.

 

Thank you for sharing.

Share this post


Link to post
Share on other sites

  • 1 month later...
  • 2 weeks later...
  • 1 month later...
  • 1 month later...

And for Prestashop 1.5 (tested on 1.5.2), just a little nuance :

 

location : override/classes/search.php

 

code

class Search extends SearchCore
{
public static function getAttributes($db, $id_product, $id_lang)
{
 if (!Combination::isFeatureActive())
  return '';
 $attributes = '';
 $attributesArray = $db->executeS('
 SELECT al.name, pa.reference FROM '._DB_PREFIX_.'product_attribute pa
 INNER JOIN '._DB_PREFIX_.'product_attribute_combination pac ON pa.id_product_attribute = pac.id_product_attribute
 INNER JOIN '._DB_PREFIX_.'attribute_lang al ON (pac.id_attribute = al.id_attribute AND al.id_lang = '.(int)$id_lang.')
 '.Shop::addSqlAssociation('product_attribute', 'pa').'
 WHERE pa.id_product = '.(int)$id_product);
 foreach ($attributesArray as $attribute)
  $attributes .= $attribute['name'].' '.($attribute['reference']!=""?$attribute['reference'].' ':'');
 return $attributes;
}

}

  • Like 1

Share this post


Link to post
Share on other sites

  • 5 months later...

Hi

 

Thanks to genix for the original fix and to deefaze for the 1.5.2 fix.

 

Anyway, for prestashop 1.5.4, you will have to modify the Search class as follows:

class SearchCore
{
	public static function getAttributes($db, $id_product, $id_lang)
	{
			/* This function was patched to return convination references:
			   * Post: [FIX] Include search by attribute references in search results
				 http://www.prestashop.com/forums/topic/175234-fix-include-search-by-attribute-references-in-search-results
			 */
			if (!Combination::isFeatureActive())
					return '';
			$attributes = '';
			$attributesArray = $db->executeS('
			SELECT al.name, pa.reference FROM '._DB_PREFIX_.'product_attribute pa
			INNER JOIN '._DB_PREFIX_.'product_attribute_combination pac ON pa.id_product_attribute = pac.id_product_attribute
			INNER JOIN '._DB_PREFIX_.'attribute_lang al ON (pac.id_attribute = al.id_attribute AND al.id_lang = '.(int)$id_lang.')
			'.Shop::addSqlAssociation('product_attribute', 'pa').'
			WHERE pa.id_product = '.(int)$id_product);
			foreach ($attributesArray as $attribute)
					$attributes .= $attribute['name'].' '.($attribute['reference'] !="" ? $attribute['reference'].' ' : '');
			return $attributes;
	}
}

 

It is escentially the same code from deefaze, but the class is not named Search any more and it doesn't extends from SearchCore.

 

By the way, I tried putting it on the "override/classes/Search.php" folder, but it didn't work. At the end I had to modify the original class.

 

Best regards

Josef

Edited by jmeile (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites

anyone know how to reference features in search results as well?

Hi jpryce

 

I see that you posted this long time ago, anyway, if you are still looking for this, in prestashop 1.5.4 is actually posible to do it. No need of changing code.

 

On the other hand, if you want to look for "Customized values", I think this isn't implemented. Anyway, I don't see any practical use for this. On my case, those values include things like size, diameter, volume, etc., so it is nothing worth to be indexed. But if you had a practical case, let me know.

 

Best regards

Josef

Share this post


Link to post
Share on other sites

  • 3 weeks later...

if you are still looking for this, in prestashop 1.5.4 is actually posible to do it. No need of changing code.

 

Hi Josef,

 

I have a clean 1.5.4.1 install, hundreeds of products loaded. References showing up on product detail pages. However, it's not search-able.

 

Anything to activate / re-generate in the back office?

 

Thanks,

Share this post


Link to post
Share on other sites

Did you already reindexed your search-engine ? Tab Preferences -> search -> index -> link reindex products

 

Thanks, I was looking under "SEO & URLs" and couldn't find my way.

 

Tested and working :)

Share this post


Link to post
Share on other sites

  • 4 months later...
  • 1 month later...

for version 1.5.6.0 I copied jmeile (#10) solution. Worked great - Thank you jmeile!

 

Don't forget to rebuild your index - Back Office Preferences -> search -> indexation -> rebuild index

 

This is bug and after a year and half since the issue was first reported I'm surprise PrestaShop still have'nt fixed this issue.

 

Chris.

Share this post


Link to post
Share on other sites

  • 2 months later...
  • 3 months later...

Following works for 1.5.x:
Edit the file /override/classes/Search.php and make it look like this:

<?php

class Search extends SearchCore
{
	
	/*
		Override: adding functionality to search by reference of attributes
	*/
	public static function getAttributes($db, $id_product, $id_lang)
	{
		if (!Combination::isFeatureActive())
			return '';

		$attributes = '';
		$attributesArray = $db->executeS('
		SELECT al.name, pa.reference FROM '._DB_PREFIX_.'product_attribute pa
		INNER JOIN '._DB_PREFIX_.'product_attribute_combination pac ON pa.id_product_attribute = pac.id_product_attribute
		INNER JOIN '._DB_PREFIX_.'attribute_lang al ON (pac.id_attribute = al.id_attribute AND al.id_lang = '.(int)$id_lang.')
		'.Shop::addSqlAssociation('product_attribute', 'pa').'
		WHERE pa.id_product = '.(int)$id_product);
		foreach ($attributesArray as $attribute)
			$attributes .= $attribute['name'] . ' ' . $attribute['reference'];
		return $attributes;
	}
	
}

Next step is to log on to your shop backend and go to "Preferences > Search" and click "Rebuild entire index".

 

Credit to posters above, I'm just clarifying what's already been told.

 

Please pay a visit to this issue on the Prestashop issue tracker and give it a vote.

Share this post


Link to post
Share on other sites

  • 2 months later...

I am working on a shop, where it is not possible to search for reference numbers in the attributes. I want to implement the above solution, but there is no /override/classes/Search.php file. My customer's shop is version 1.5.6.1

 

So I checked my own store which runs 1.5.6.2 and I don't have this file either - but I can search in attributes just fine.

 

Has something changed in the versions since the solutions above?

 

I have of course rebuilt the index on the customer's shop, so that is not the problem.

Share this post


Link to post
Share on other sites

  • 5 months later...

Following works for 1.5.x:

Edit the file /override/classes/Search.php and make it look like this:

<?php

class Search extends SearchCore
{
	
	/*
		Override: adding functionality to search by reference of attributes
	*/
	public static function getAttributes($db, $id_product, $id_lang)
	{
		if (!Combination::isFeatureActive())
			return '';

		$attributes = '';
		$attributesArray = $db->executeS('
		SELECT al.name, pa.reference FROM '._DB_PREFIX_.'product_attribute pa
		INNER JOIN '._DB_PREFIX_.'product_attribute_combination pac ON pa.id_product_attribute = pac.id_product_attribute
		INNER JOIN '._DB_PREFIX_.'attribute_lang al ON (pac.id_attribute = al.id_attribute AND al.id_lang = '.(int)$id_lang.')
		'.Shop::addSqlAssociation('product_attribute', 'pa').'
		WHERE pa.id_product = '.(int)$id_product);
		foreach ($attributesArray as $attribute)
			$attributes .= $attribute['name'] . ' ' . $attribute['reference'];
		return $attributes;
	}
	
}

Next step is to log on to your shop backend and go to "Preferences > Search" and click "Rebuild entire index".

 

Credit to posters above, I'm just clarifying what's already been told.

 

Please pay a visit to this issue on the Prestashop issue tracker and give it a vote.

 

This worked perfectly for me. Prestashop 1.6.0.8. I've edited the file /classes/Search.php and I had to rebuild the index.

 

So thanks :)

Share this post


Link to post
Share on other sites

  • 1 year later...

Cant believe they still haven't fixed this issue, but thanks for helping me out, worked like a charm for 1.6.1.4.

 

Now the only thing I need to figure out is how to make the same thing work in backend search for reference, any ideas?

Share this post


Link to post
Share on other sites

  • 9 months later...
  • 3 years later...
On 5/15/2017 at 11:13 AM, Tatort said:

Hi Guys!

 

Someone know how to see the searched variation and not the default variation ?

I don't find a way...

 

Thanks!

 

Romain

Hello,

I have the same question. How to show the specific variation as a result and not the default one?

PS 1.7.6

Share this post


Link to post
Share on other sites

  • 1 year later...

Hello, I'm currently on version 1.7.8.3, I tried this solution but unfortunately it didn't work. I didn't have a Search.php file in Override, so I tried modifying the one in Classes.
Has anyone fixed the issue recently? This is a pretty annoying one, hard to believe that there hasn't been yet an official solution.
If not by fixing this issue, how is it possible to achieve the same thing (find a Combination by it's reference number)?
Thanks in advance

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More