You are here:  » Display Preferred Merchant Image & Ratings in Search Results


Display Preferred Merchant Image & Ratings in Search Results

Submitted by npaitken on Wed, 2010-01-27 17:44 in

Hi David,

A couple of things that I've been thinking about to improve search results.

Display preferred merchant image

Some images are really small/poor on certain feeds on my site. I was wondering if it's possible to have more control over which merchant's image displays where there is more than 1 merchant. This applies to search results and product page? Either by promoting selected merchant(s) or suppressing bad merchant, is it possible to do this?

Display Product Rating Stars in Search Results

I'm sure this was discussed before somewhere. How do I display product rating stars in search results with link to review. So each search item (with a rating) would look something like:

TITLE
XXXXX Read Reviews
IMAGE
from PRICE
more infrmation

Many thanks,
Neil

Submitted by support on Thu, 2010-01-28 09:37

Hi Neil,

Sure - here is a function I have written to provide more control over images. Insert the code below into the top of includes/tapestry.php(immediately after the opening PHP tag):

  function tapestry_getImage($name,$product=NULL)
  {
    global $config_databaseTablePrefix;
    $merchants = array("Merchant 1","Merchant 2","Merchant 3");
    if (is_array($product))
    {
      if ($product["image_url"] && ($product["merchant"]==$merchants[0]))
        return $product["image_url"];
    }
    foreach($merchants as $merchant)
    {
      $sql = "SELECT image_url FROM `".$config_databaseTablePrefix."products`
                WHERE
                image_url <> ''
                AND
                name='".database_safe($name)."'
                AND
                merchant='".database_safe($merchant)."'
                ";
      if (database_querySelect($sql,$result))
      {
        return $result[0]["image_url"];
      }
    }
    if (is_array($product))
    {
      if ($product["image_url"]) return $product["image_url"];
    }
    return "";
  }

To use this, first edit the $merchants array (5th line of the above code) to contain your preferred image merchants in order or preference (best images first). Then, to implement for search results; in html/searchresults.php, look for the following code on line 7:

<?php foreach($searchresults["products"] as $product): ?>

...and REPLACE with:

<?php foreach($searchresults["products"] as $product): ?>
<?php $product["image_url"] = tapestry_getImage($product["name"],$product); ?>

...and to implement on the product page, look for the following code on line 2 of html/product.php:

  $product_main = $product["products"][0];

...and REPLACE with

  $product_main = $product["products"][0];
  $product_main["image_url"] = tapestry_getImage($product_main["name"],$product_main);

For completeness, the same can be applied to featured products. In this case, look for the following code around line 7 of html/featured.php:

<?php foreach($featured["products"] as $product): ?>

...and REPLACE with:

<?php foreach($featured["products"] as $product): ?>
<?php $product["image_url"] = tapestry_getImage($product["name"],$product); ?>

The code is optimised so that if the image URL is already from the preferred merchant it is returned quickly, so there are no unnecessary database calls.

Regarding the rating stars being a link to the reviews page and also showing the number of reviews; look for the following code which currently displays the star rating on its own, within line 16 of html/searchresults.php

<?php if ($product["rating"]) print tapestry_stars($product["rating"],""); ?>

...and REPLACE with:

<?php
if ($product["rating"])
{
  if (
$config_useRewrite)
  {
    
$reviewHREF $config_baseHREF."review/".tapestry_hyphenate($product["name"]).".html";
  }
  else
  {
    
$reviewHREF $config_baseHREF."reviews.php?q=".urlencode($product["name"]);
  }
  print 
tapestry_stars($product["rating"],"");
  print 
"&amp;";
  print 
"<a href='".$reviewHREF."'>Read Reviews</a>";
}
?>

Hope this helps!

Cheers,
David.

(Edit: Line numbering and namespaces updated for 15/09A)

Submitted by npaitken on Thu, 2010-01-28 10:04

Hi David,

Many thanks for this. Just working my through it now. I don't seem to have includes/searchresults.php - is this part of the new distribution?

Thanks,
Neil

Submitted by npaitken on Thu, 2010-01-28 10:22

Hi David,

Just implemented the Ratings and it looks great. Is it possible to change it from

Read Reviews

to have the number of reviews that exist. For instance

1 Review; or
2 Reviews
etc

Thanks,
Neil

Submitted by support on Thu, 2010-01-28 11:54

Hi Neil,

Sorry - it should have been html/searchresults.php - i've corrected it above.

Regarding the number of reviews in the text; sure - instead of this line:

print "<a href='".$reviewHREF."'>Read Reviews</a>";

Use:

print "<a href='".$reviewHREF."'>".$product["reviews"]." Review".(($product["reviews"]>1)?"s":"")."</a>";

Cheers,
David.

Submitted by npaitken on Thu, 2010-01-28 13:42

Hi David,

Got the reviews mod completed. Many thanks it looks great.

Regarding the preferred image mod. I've implemented it but it doesn't seem to be working yet. I've added the function to includes/tapestry.php and codes snippets to html/searchresults.php and html/product.php. However, when I do a search the images haven't changed to preferred merchants. Also, main image on product pages has disappeared now!

Any thoughts on what to check first?

Thanks,
Neil

Submitted by support on Thu, 2010-01-28 13:53

Hi Neil,

There was a mistake in the replacement code for html/product.php - i've corrected it above; so that should restore the image on the product page.

Could you perhaps email me your modified includes/tapestry.php and html/searchresults.php, and a link to search results that are not swapping out the images as you would expect and I'll take a look for you...!

Cheers,
David.

Submitted by bat on Fri, 2012-11-09 16:46

Hi David
I would like to show the star rating in the search results but the above code isn't working for me.
I wonder if it's because you very kindly combined my reviews.php into my products.php for me?

How would I get the stars to appear, please?

Thanks,
Bat

Submitted by support on Fri, 2012-11-09 17:02

Hi Bat,

Search results should be displaying the star rating graphic by default - it's this code which is within line 16 of html/searchresults.php

<?php if ($product["rating"]) print tapestry_stars($product["rating"],""); ?>

...so any changes to your product / review merge shouldn't be affected this at all.. Check whether the above section of code is in place in your html/searchresults.php, and if not it can be added anywhere within the FOREACH construct that displays each search result.

Otherwise, if you're not sure if you would like to email me your search.php and html/searchresults.php I'll check it out for you...

Cheers,
David.
--
PriceTapestry.com