You are here:  » Image - Choose prefered merchant


Image - Choose prefered merchant

Submitted by Mahony on Wed, 2012-01-18 20:44 in

Hi David,

I have about 15 merchants for the same product to compare prices.
The image that gets displayed is the image of the cheapest merchant.

My Problem is that some merchants have a slow server connection,
or just really bad quality images.

Thats the reason I want to choose one prefered merchant and two alternative merchants
(if the prefered merchant doesn't sell the product) to pull the image from,
and not from the cheapest merchant.
(Only if the prefered and alternative merchants dont sell the product).

Is there a way to do this?

Big thanks in advance David!

Cheers,
Tony

Submitted by support on Thu, 2012-01-19 08:55

Hi Tony,

Sure - have a go as follows. In html/product.php look for the following code at line 2:

  <?php $mainProduct $product["products"][0]; ?>

...and REPLACE with:

  <?php $mainProduct $product["products"][0]; ?>
  <?php
  $imageMerchants = array("Merchant 1","Merchant 2","Merchant 3");
  $done = FALSE;
  foreach($imageMerchants as $imageMerchant)
  {
    foreach($product["products"] as $v)
    {
      if (($v["merchant"]==$imageMerchant) && ($v["image_url"]))
      {
        $mainProduct["image_url"] = $v["image_url"];
        $done = TRUE;
        break;
      }
    }
    if ($done) break;
  }
  ?>

Simply replace Merchant 1, Merchant 2 etc. with your preferred image merchants in order of priority...

Cheers,
David.
--
PriceTapestry.com

Submitted by Mahony on Thu, 2012-01-19 10:37

I'm sorry.. I should have told you before that I'm using the PTO plugin.
What do I have to replace there?

Thank you David!
Tony

Submitted by support on Thu, 2012-01-19 11:23

Hi Tony,

No problem - to do the equivalent modification in the plugin, look for the following code at line 286 of pto_product.php:

    $html_product = $pto_html_product;

...and REPLACE with:

    $imageMerchants = array("Merchant 1","Merchant 2","Merchant 3");
    $done = FALSE;
    foreach($imageMerchants as $imageMerchant)
    {
      for($i=0;$i<$numRows;$i++)
      {
        $v = $wpdb->last_result[$i];
        if (($v->merchant==$imageMerchant) && ($v->image_url))
        {
          $product->image_url = $v->image_url;
          $done = TRUE;
          break;
        }
      }
      if ($done) break;
    }
    $html_product = $pto_html_product;

Cheers,
David.
--
PriceTapestry.com

Submitted by Mahony on Thu, 2012-01-19 15:24

Thank you a lot David! :)

Cheers,
Tony

Submitted by bizzo on Wed, 2016-03-16 03:08

Hi David,
I was wondering if it would be possible to apply this to the search results pages as well? I tried to edit the code but couldn't get it to work, would you be able to give me a few pointers please?

Thanks
gavin

Submitted by support on Wed, 2016-03-16 10:19

Hello Gavin and welcome to the forum!

It's a little trickier for search results since the result set comes from a summary query so there is only a single image_url value present in each result. This means a re-query would be required, but it can be done in a single query by compiling an appropriate WHERE clause to select product names and image URLs from all merchants ordered by a merchant priority (sequence in the $imageMerchants array). Have a go with the following code inserted at the top of html/searchresults.php:

<?php
  $imageMerchants 
= array("Merchant 1","Merchant 2","Merchant 3");
  
$merchantIns = array();
  
$sqlCase "CASE merchant";
  foreach(
$imageMerchants as $k => $imageMerchant)
  {
    
$merchantIns[] = "'".database_safe($imageMerchant)."'";
    
$sqlCase .= " WHEN '".database_safe($imageMerchant)."' THEN ".$k;
  }
  
$sqlCase .= " END AS priority";
  
$productIns = array();
  foreach(
$searchresults["products"] as $product)
  {
    
$productIns[] = "'".database_safe($product["name"])."'";
  }
  
$merchantIn implode(",",$merchantIns);
  
$productIn implode(",",$productIns);
  
$sql "SELECT name,image_url,".$sqlCase."
            FROM `"
.$config_databaseTablePrefix."products`
              WHERE merchant IN ("
.$merchantIn.") AND name IN (".$productIn.")
                AND image_url <> '' ORDER BY priority"
;
  
database_querySelect($sql,$rows);
  
$imageOverrides = array();
  foreach(
$rows as $row)
  {
    if (!isset(
$imageOverrides[$row["name"]]))
    {
      
$imageOverrides[$row["name"]] = $row["image_url"];
    }
  }
  foreach(
$searchresults["products"] as $k => $product)
  {
    if (isset(
$imageOverrides[$product["name"]]))
    
$searchresults["products"][$k]["image_url"] = $imageOverrides[$product["name"]];
  }
?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by stevebi on Thu, 2016-03-17 07:44

Hi,

It may help to use http://www.pricetapestry.com/node/3250

Cheers

Steve

Submitted by bizzo on Mon, 2016-03-21 02:43

Thanks David, that works perfectly.

Cheers
Gavin

Submitted by zway on Mon, 2016-04-18 01:20

I've update this code for the latest version.

  $product_main = $product["products"][0];
  $imageMerchants = array("Merchant1");
  $done = FALSE;
  foreach($imageMerchants as $imageMerchant)
  {
    foreach($product["products"] as $v)
    {
      if (($v["merchant"]==$imageMerchant) && ($v["image_url"]))
      {
        $product_main["image_url"] = $v["image_url"];
        $done = TRUE;
        break;
      }
    }
    if ($done) break;
  }

If you want to prioritise name you can use the code below.

$done = FALSE;
foreach($nameMerchants as $nameMerchant)
  {
    foreach($product["products"] as $v)
    {
      if (($v["merchant"]==$nameMerchant) && ($v["name"]))
      {
        $product_main["name"] = $v["name"];
        $done = TRUE;
        break;
      }
    }
    if ($done) break;
  }