1

I am trying to display amount saved on the catalogue page for a magento 2.0.2 website. However I'm not getting the calculation result displayed. I just get an empty space. I am editing the final_price.phtml in my theme file. Please can I get some advice on what I am doing wrong. I can't find any information from searches as most results are for magento 1 and the codes are throwing errors This is what my code looks like in the section which i am trying to do the calculations.

<span class="special-price"><span class="only-text">Only: </span>
    <?php echo $block->renderAmount($finalPriceModel->getAmount(), [
        'display_label'     => __('Special Price'),
        'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
        'price_type'        => 'finalPrice',
        'include_container' => true,
        'schema' => $schema
    ]); ?>
</span>
<br>
<span class="old-price"><span class="rrp-text">RRP: </span>
    <?php echo $block->renderAmount($priceModel->getAmount(), [
        'display_label'     => __('Regular Price'),
        'price_id'          => $block->getPriceId('old-price-' . $idSuffix),
        'price_type'        => 'oldPrice',
        'include_container' => true,
        'skip_adjustments'  => true
    ]); ?>
</span>
<span class="saving-price"><span class="saving-text">Saving: </span>
<?php
$wasPrice = $block->renderAmount($priceModel->getAmount(), []);
$nowPrice = $block->renderAmount($finalPriceModel->getAmount(), []);
  if ($nowPrice < $wasPrice){
    $saving = $wasPrice - $nowPrice; 
    echo $saving;
  }
?>
</span>

1 Answer 1

3

I ran into the same issue on category and search results pages on Magento 2. Here's what I eventually figured out. Hope it helps! (Note: this all falls within the foreach loop which iterates through each $_product within $_productCollection):

<?php
$regprice = $_product->getPrice();
$specialprice = $_product->getSpecialPrice();
$yousave = number_format((float)($regprice - $specialprice), 2, '.', '');
$yousavepct = number_format((float)(100*(($regprice - $specialprice)/$regprice)), 0);

if($yousave > 0): ?>
    <p class="you-save-statement">You save: $<?php echo $yousave; ?> (<?php echo $yousavepct; ?>%)</p>
<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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