You are here:  » Savings calculation


Savings calculation

Submitted by IG on Tue, 2015-04-07 14:01 in

Hi David

I have added "normalprice as extra field to the database and I have also added the necessary code to config.advanced,php. So far so good.

Now what I have is a field "price", which is the discounted price in decimalised format (for example 10.20) and I have a field "normalprice" for the regular price, but which is in the format 1520, which means the price needs to be divided by 1000 to have the same format as "price", meaning 15.20.

The aim is to show "normalprice", "price" and "savings" in % for each product.

My questions:
1) How and where do I divide "normalprice" by 1000?
2) Do you advise to create an additional field "savings" in the database or does it make more sense to do the calculation on page? How and where would I calculate the "savings" in %?

As ussual, I look forward to your wise guidance.

Kind regards,
Ivo

Submitted by support on Tue, 2015-04-07 14:38

Hello Ivo,

If you only have a cent value for `normalprice`, the most straight forward thing to do is to hard code the division, and also use tapestry_decimalise() on the value.

To do this, edit includes/admin.php and look for the following code at line 459:

  $importRecord["price"] = tapestry_decimalise($importRecord["price"]);

...and REPLACE with:

  $importRecord["price"] = tapestry_decimalise($importRecord["price"]);
  $importRecord["normalprice"] = tapestry_decimalise($importRecord["normalprice"]/100);

With that in place, savings can be calculated at display time no problem. The normal price would be displayed as follows:

Normal Price: <?php print tapestry_price($product_main["normalprice"]); ?>

(As of 15/01A, the tapestry_price() function displays the price using the currency HTML, decimal separator and order as configured in config.advanced.php)

To calculate and show the saving, if there is one have a go with the following in html/product.php:

<?php
  
if ($product_main["normalprice"] > $product_main["price"])
  {
    
$saving = (($product_main["normalprice"] - $product_main["price"]) / $product_main["normalprice"]) * 100;
    print 
"<p>Saving: ".$saving."%</p>";
  }
?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by IG on Tue, 2015-04-07 16:03

Hi David

All clear and understood. Thanks a bunch.

Unfortunately, I did not think this completely through from my side (no surprise there). Some feeds will have cent values (1500) and some feeds will have regular values (15.00) for 'normalprice'. I guess hardcoding is no longer a good option, but a filter might work. How would I create a filter that divides by 100? Or is there an easier solution that I cannot think of?

Kind regards,
Ivo

Submitted by support on Tue, 2015-04-07 16:16

Hello Ivo,

There's a Div100 filter in this comment...

Cheers,
David.
--
PriceTapestry.com

Submitted by IG on Tue, 2015-04-07 17:47

You are a star!