You are here:  » Min and max price on product page


Min and max price on product page

Submitted by Bakalinge on Wed, 2013-02-20 16:02 in

Hi David,

I'm trying to display the min and max prices for a product. This is what I have on product.php

<?php if ($product["numMerchants"] > 1): ?>
<div itemprop='offers' itemscope itemtype='http://schema.org/AggregateOffer'>
<?php print translate("Prices"); ?>: <span itemprop='lowPrice'><?php print $mainProduct["minPrice"]."&nbsp;".$config_currencyHTML?></span> - <span itemprop='highPrice'><?php print $mainProduct["maxPrice"]."&nbsp;".$config_currencyHTML?></span>
</div>
<?php else: ?>
<?php print translate("Price"); ?>: <?php print $mainProduct["price"]."&nbsp;".$config_currencyHTML?>
<?php endif; ?>

but for now, I only get 1 price (the cheapest one), even if numMerchants > 1

Could you please share your enlightened opinion about this ?

Thank you !
Bak

Submitted by support on Wed, 2013-02-20 16:15

Hi Bak,

The summary values (numMerchants, minPrice etc.) are not present in product results but they can be easily derived from $product["products"], have a go with the following:

<?php if (count($product["products"]) > 1): ?>
<div itemprop='offers' itemscope itemtype='http://schema.org/AggregateOffer'>
<?php print translate("Prices"); ?>: <span itemprop='lowPrice'><?php print $product["products"][0]["price"]."&nbsp;".$config_currencyHTML?></span> - <span itemprop='highPrice'><?php print $product["products"][(count($product["products"])-1)]["price"]."&nbsp;".$config_currencyHTML?></span>
</div>
<?php else: ?>
<?php print translate("Price"); ?>: <?php print $mainProduct["price"]."&nbsp;".$config_currencyHTML?>
<?php endif; ?>

Summary of the change, the number of merchants is returned by:

count($product["products"])

Lowest price:

$product["products"][0]["price"]

Highest price:

$product["products"][(count($product["products"])-1)]["price"]

Cheers!
David.
--
PriceTapestry.com

Submitted by Bakalinge on Wed, 2013-02-20 17:27

Thank you David !

Submitted by ChrisNBC on Thu, 2014-03-13 14:23

Hi David,

I have just included the above code in my product.php page. The lowest and highest prices work as expected but nothing is displayed for number of merchants? I wondered if you might be able to suggest what could be causing this?

Thanks in advance.

Regards
Chris

Submitted by support on Thu, 2014-03-13 14:38

Hi Chris,

The above code doesn't display the number of merchants at all, it's only used to access the highest price. However, it is easy to display - wherever you want to display the merchant count on your product page (html/product.php), use something like:

Comparing <?php print count($product["products"]); ?> prices!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2014-03-14 14:36

Hi David,

Thanks for your quick response. I have tried the code above and it returns total products fine but when I modify it to return total merchants I just get '0' displayed? Am I correct I my understanding that I just need to change the code to the line below to return the total merchants quoting for the product in focus?

Comparing

<?php
 
print count($product["merchant"]); 
?>
prices!

Additionally, please could you tell me if it's possible to split the results returned by the above using the test below so that I can display the results split into type1, type2 and type3 products?

I tried to add the code to product.php but just got errors...

<?php
  
foreach($prices["products"] as $product)
  {
    if (
$product["paymonthly"]=="yes")
    {
      
$type 1;
    }
    elseif (
$product["payg"]=="yes")
    {
      
$type 2;
    }
    elseif (
$product["simo"]=="yes")
    {
      
$type 3;
    }
    
$prices[$type][] = $product;
  }
?>

Thanks in advance.

Regards
Chris

Submitted by support on Sat, 2014-03-15 09:10

Hi Chris,

In terms of counts by type and merchant, since you have separate variables rather than a single type field the best thing to do is simply to loop building up counters for each type - have a go with something like:

<?php
  $paymonthlyCount 
0;
  
$paymonthlyMerchants = array();
  
$paygCount 0;
  
$paygMerchants = array();
  
$simoCount 0;
  
$simoMerchants = array();
  
$allMerchants = array();
  foreach(
$prices["products"] as $p)
  {
    
$allMerchants[$p["merchant"]] = TRUE;
    if (
$product["paymonthly"]=="yes")
    {
      
$paymonthlyCount++;
      
$paymonthlyMerchants[$p["merchant"]] = TRUE;
    }
    elseif (
$product["payg"]=="yes")
    {
      
$paygCount++;
      
$paygMerchants[$p["merchant"]] = TRUE;
    }
    elseif (
$product["simo"]=="yes")
    {
      
$simoCount++;
      
$simoMerchants[$p["merchant"]] = TRUE;
    }
  }
?>

And to display, use:

<p>Pay Monthly: <?php print $paymonthlyCount?> offers from <?php print count($paymonthlyMerchants); ?> merchants</p>
<p>PAYG: <?php print $paygCount?> offers from <?php count($paygMerchants); ?> merchants</p>
<p>SIM Only: <?php print $simoCount?> offers from <?php count($simoMerchants); ?> merchants</p>

Total number of offers is as before, and from the above loop total number of unique merchants is count($allMerchants) - e.g.

<p>Comparing <?php print count($product["products"]); ?> offers from <?php count($allMerchants); ?> merchants</p>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Mon, 2014-03-17 10:07

Hi David,

Hope you had a good weekend. Thanks for your response.

I have added the code to product.php but the results show 0 offers from 0 merchants? I wondered if you could suggest how I might resolve this?

Additionally, please could you confirm how I can display the lowest and highest prices in context for the three types?

Thanks in advance.

Regards
Chris

Submitted by support on Mon, 2014-03-17 12:25

Hi Chris,

Code above was based on being included in includes/prices.php. To include in html/product.php instead, simply REPLACE this line:

  foreach($prices["products"] as $p)

...with:

  foreach($product["products"] as $p)

To add lowest and highest price for each type, try the following, based on the previous example (and including above modification, and slightly reworked:

<?php
  $paymonthlyCount 
0;
  
$paymonthlyProducts = array();
  
$paymonthlyMerchants = array();
  
$paygCount 0;
  
$paygProducts = array();
  
$paygMerchants = array();
  
$simoCount 0;
  
$simoProducts = array();
  
$simoMerchants = array();
  
$allMerchants = array();
  foreach(
$prices["products"] as $p)
  {
    
$allMerchants[$p["merchant"]] = TRUE;
    if (
$product["paymonthly"]=="yes")
    {
      
$paymonthlyProducts[] = $p;
      
$paymonthlyMerchants[$p["merchant"]] = TRUE;
    }
    elseif (
$product["payg"]=="yes")
    {
      
$paygProducts[] = $p;
      
$paygMerchants[$p["merchant"]] = TRUE;
    }
    elseif (
$product["simo"]=="yes")
    {
      
$simoProducts[] = $p;
      
$simoMerchants[$p["merchant"]] = TRUE;
    }
  }
  
$paymonthlyCount count($paymonthlyProducts);
  
$simoCount count($simoProducts);
  
$paygCount count($paygProducts);
  function 
pcmp($a$b)
  {
    if (
$a["price"] == $b["price"]) {
        return 
0;
    }
    return (
$a["price"] < $b["price"]) ? -1;
  }
  
usort($paymonthlyProducts,"pcmp");
  
usort($paygProducts,"pcmp");
  
usort($simoProducts,"pcmp");
  
$paymonthlyMin $paymonthlyProducts[0]["price"];
  
$paymonthlyMax $paymonthlyProducts[($paymonthlyCount-1)]["price"];
  
$paygMin $paygProducts[0]["price"];
  
$paygMax $paygProducts[($paygCount-1)]["price"];
  
$simoMin $simoProducts[0]["price"];
  
$simoMax $simoProducts[($simoCount-1)]["price"];
?>

And then you can print any of the derived variables as normal, e.g.

<p>Pay Monthly deals from <?php print $paymonthlyMin?> to <?php print $paymonthlyMax?></p>

Note the above becomes quite repetitive due to having multiple type = ("yes" or "no") fields rather than type = ("paymonthly" or "payg" or "simo"). If you're close to your goal for your product / price comparison table for this project it won't be worth changing the structure now, but if you were looking to add quite a bit more dependent functionality let me know and I'll help you restructure the product array into a single rather than multiple type fields...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Mon, 2014-03-17 12:50

Hi David,

Thanks for the quick response. Thank you also for your kind offer to help me modify the code. I have almost met my goal for this site so will probably leave the code as is.

I have changed the code but it does not appear to work and the error below is displayed:

"Fatal error: Cannot redeclare cmp() (previously declared in {code saved}"

Would be grateful if you could suggest how this might be resolved.

Thanks in advance.,

Regards
Chris

Submitted by support on Mon, 2014-03-17 13:16

Hi Chris,

No problem!

Regarding cmp() redeclaration - in the new code just search and replace each instance of "cmp" with "pcmp" (as I've done above) that will be all you need to do...

Cheers,
David.
--
PriceTapestry.com