1

Im wanting to replicate this pricing look in Magento 2.

Pricing example

This is the code I used in our magento 1 to accomplish this.

            <?php $special_price = $_product->getSpecialPrice(); $regular_price = $_product->getPrice(); ?>
            <?php $saved_amount = $regular_price - $special_price; $saved_percent = 100 * $saved_amount / $regular_price; ?>
        <span class="yousave">Save: <?php echo '$'.number_format($saved_amount,0).' ('.number_format($saved_percent,0).'%)'; ?></span>

I am looking to put the equivalent of this in Magento 2.2.5. Magento 2 is using the code below to get pricing. I have been editing /Magento_Catalog/templates/product/price/final_price.phtml and noticed that M2 handles it differently and I have had no luck coming up with how to do this.

<?php if ($block->hasSpecialPrice()): ?>
    <span class="old-price">
        <?php /* @escapeNotVerified */ 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="special-price">
        <?php /* @escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [
            'display_label'     => __('Special Price'),
            'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
            'price_type'        => 'finalPrice',
            'include_container' => true,
            'schema' => $schema
        ]); ?> 
<?php else: ?>
    <?php /* @escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [
        'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
        'price_type'        => 'finalPrice',
        'include_container' => true,
        'schema' => $schema
    ]); ?>
<?php endif; ?>

How can I accomplish this in the final_price.phtml to reflect how I have it in working in Magento 1?

0

Used this code in Magento 2.3.x that I inserted into final_price.phtml:

<?php 
    $oldRenderedPrice = $priceModel->getValue();
    $finalRenderedPrice =  $finalPriceModel->getValue();

    if ($oldRenderedPrice > $finalRenderedPrice){
?>
        <div class="saved-label">
<?php
        $amountSaved = number_format(($oldRenderedPrice - $finalRenderedPrice), 2, '.', '');
        $percentSaved = number_format((100*(($oldRenderedPrice - $finalRenderedPrice)/$oldRenderedPrice)), 0);

        echo "Saves: $" . $amountSaved . " (" . $percentSaved . "% Off)";
?>
        </div>
<?php
    }
?>

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.