Magento 2 – Show Normal and Special Price on Minicart

magento-2.1mini-cartoverridesprice

I can set Normal price along with Special price while put custom function in core file

vendor/magento/module-weee/Block/Item/Price/Renderer.php

public function getUnitItemPriceExclTax()
{
    $priceExclTax = $this->getItem()->getProduct()->getPrice();

    return $priceExclTax;
}

And calling this function into core file,
vendor/magento/module-weee/view/frontend/templates/checkout/cart/item/price/sidebar.phtml

So getting both price correctly, but i want to override that

vendor/magento/module-weee/Block/Item/Price/Renderer.php block on my custom module.

I have created di.xml with below code :

<preference for="Magento\Weee\Block\Item\Price\Renderer" type="<namespace\<module_name>\Block\Item\Price\Renderer"/>

And just put that getUnitItemPriceExclTax() function in that block.

enter image description here

Best Answer

You dont need to override Renderer.php file in core and you should never do in core. You can just changes in sidebar.phtml file and set your changes.

You can get price from below method:

$finalPrice = $item->getProduct()->getFinalPrice();
$normalPrice = $item->getProduct()->getPrice();

After getting above changes you can do it below code in your template file:

<?php if ($block->displayPriceWithWeeeDetails()): ?>
        <span class="minicart-tax-total">
    <?php else: ?>
        <span class="minicart-price">
    <?php endif; ?>
        <?php /* @escapeNotVerified */ echo $block->formatPrice($block->getUnitDisplayPriceExclTax()); ?> 
        </span>

    <?php if($normalPrice != $finalPrice){ ?>
    <span class="minicart-old-price">
            <?php /* @escapeNotVerified */ echo $block->formatPrice($normalPrice); ?>
    </span>
    <?php }   ?>

I have done changes in Magento Version 2.1.1.