I've confused myself enough with this one - no idea what I'm doing wrong.

So we enter the MSRP for products in Magento and display this price in the front end with a strike through.

In this example, please see our prices:-

<span class="final-price">521.33</span> <!-- <?php echo $_finalPrice ?> -->
<span class="final-price-including-tax">625.6</span> <!-- <?php echo $_finalPriceInclTax ?> -->
<span class="regular-price">625.6</span><!-- <?php echo $_regularPrice ?> -->
<span class="msrp-price">£736.00</span> <!-- <?php echo $_msrpPrice ?> -->

We currently have:-

£625.60 (Ex VAT: £521.33) RRP: £736.00

As can be seen like so:-

MSRP in Magento

We wish to show next to the RRP ($_msrpPrice), 'You Save: £110.40 (%15)'.

For this, what we have is:-

<?php $getpercentage = number_format($_finalPrice / $_regularPrice * 100, 2); $finalpercentage = 100 - $getpercentage;?>

<p class="you-save-price">
<span class="price-label"><?php echo $this->__('You Save:') ?></span>
<span class="price" id="product-price-<?php echo $_id ?><?php echo $this->getIdSuffix() ?>"><?php echo '&pound;' . number_format(abs($_finalPrice - $_regularPrice),2); ?> <?php echo '(' . number_format($finalpercentage, 0) . '%)'; ?></span>

But this is returning the saving of £104.27 (17%) which is calculated from the ex tax price minus inc tax price. What we require is the difference between $_msrpPrice and $_regularPrice.

I've tried countless variations to try and get this calculation right but mostly these are returning 0.00 or 625.60.

This for example:-

<?php echo '&pound;' . number_format(abs($_msrpPrice - $_regularPrice),2) . '!'; ?>

Just returns 625.60.

What should the calculation be to get said difference between msrp and regular price (price including the tax)?

Update from @David Manners answer...

Tried applying your suggestion with the below code:-

    $total_saving_percent = number_format(100 - ($_finalPriceInclTax / $_msrpPrice * 100), 2);
    $total_saving = $_msrpPrice - $_finalPriceInclTax;
<p class="you-save-price">
    <span class="price-label"><?php echo $this->__('You Save:') ?></span>
    <span class="price" id="product-price-<?php echo $_id ?><?php echo $this->getIdSuffix() ?>">
        <?php //echo $total_saving ?><?php //echo $total_saving_percent ?>
        <?php //echo '&pound;' . number_format(abs($_finalPrice - $_regularPrice),2); ?> <?php //echo '(' . number_format($finalpercentage, 0) . '%)'; ?>
        <?php echo '&pound;' . number_format(abs($total_saving),2); ?> <?php echo '(' . number_format($total_saving_percent, 0) . '%)'; ?>

And this outputted the below:-

Magento Price Saving

Thinking there is a 100% saving.

Can you spot an error in what I have done?

Edit: Answer working from @David Manners' help...

(adding to question so that @David Manners' can update his answer).

The currency call in $_msrpPrice appeared to be affecting the calculation of $_msrpPrice - $_finalPriceInclTax. Adding a new variable like the below that excluded the currency call allowed the saving calculation to work (I recommend adding a new variable excluding currency in $_msrpPrice rather than modifying the existing other it will break other places on your Magento store where this is in use).

    $newmsrp = $this->helper('core')->currency($_product->getMsrp(),false,false);
    $total_saving_percent = number_format(100 - ($_finalPriceInclTax / $newmsrp * 100), 2);
    $total_saving = $newmsrp - $_finalPriceInclTax;

With this new variable in place and the savings calculation using thus, we can render the necessary at the front end using:-

<p class="you-save-price">
    <span class="price-label"><?php echo $this->__('You Save:') ?></span>
    <span class="price" id="product-price-<?php echo $_id ?><?php echo $this->getIdSuffix() ?>">
        <?php echo '&pound;' . number_format(abs($total_saving),2); ?> <?php echo '(' . number_format($total_saving_percent, 0) . '%)'; ?>

Which at last, outputs the savings correctly on a per product basis:-

Magento Product Savings Calculation

  • ok now I am really confused if it shows the final price and msrp correctly how is the simple msrp-final returning just the final price with the values shown in your new screen shot :( maybe if you include the phtml file from each price display that might help. Oct 1 '13 at 15:33
  • Yeh - I might have something funky going on in the template then... Anyhow, here is my price.phtml. You can see what I'm doing at lines 157 - 196. Thanks.
    – zigojacko
    Oct 1 '13 at 15:43
  • From you code I could not see anything obviously wrong. I have copied out the calculation section and used it in a simple php file and it is creating the correct values. I would suggest logging what you actually have for prices before you do the calculation and see if you have any issues. Oct 1 '13 at 16:04
  • For the latest screenshot, it's literally just 'Price: 83.00' and 'MSRP: 110.00'. That's the only prices set. Thanks for taking a look.
    – zigojacko
    Oct 1 '13 at 16:11
  • :( sorry it is not working. I have updated my answer with the full snippet I used Oct 1 '13 at 16:13

The problem was that the Msrp price was put through the formatting function currency. This made the calculation fail because it added extra information and html. If you set the second and third parameters to false this will fix the issue. The second will disable the formatting of the price in the sense of currency formatting. The third will exclude the html wrapped <span class="price"> from the returned price.

$newmsrp = $this->helper('core')->currency($_product->getMsrp(),false,false);

Either that or you can ignore the currency function for the calculation and just use the price returned from getMsrp:

$_finalPriceInclTaxForCalc = $_taxHelper->getPrice($_product, $_product->getFinalPrice(), true)
$_msrpPriceForCalc = $_product->getMsrp();
$total_saving_percent = number_format(100 - ($_finalPriceInclTaxForCalc / $_msrpPriceForCalc  * 100), 2);
$total_saving = $_msrpPriceForCalc - $_finalPriceInclTaxForCalc;

echo '&pound;' . number_format(abs($total_saving),2) . ' (' . number_format($total_saving_percent, 0) . '%)';

This will give you the output £27.00 (25%)

  • That didn't seem to apply the right calculation either no. Updating my question with screenshot and code used as a result of your answer - thanks.
    – zigojacko
    Oct 1 '13 at 15:13
  • Bizarre. I've just echoed out $_finalPriceInclTax which returns 83. I've just echoed out $_msrpPrice which returns 110. And apparently $_msrpPrice - $_finalPriceInclTax = 83 ($_finalPriceInclTax). Doesn't even make sense :/
    – zigojacko
    Oct 1 '13 at 16:43
  • It is $total_saving = $_msrpPrice - $_finalPriceInclTax; that isn't doing anything I think.
    – zigojacko
    Oct 1 '13 at 16:54
  • I would just log everything so you can be 100% at which point it fails. Oct 1 '13 at 16:57
  • Got it! The $_msrpPrice is formatted with currency :) Oct 1 '13 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.