Magento – How to display discount on Special price in product detail page

magento2productproduct-viewspecial-price

I overridden module-catalog/product/price/amount/default.phtml file in my theme

<?php 
    echo $this->helper('Test\ProductDetail\Helper\Data')->DisplayDiscountLabel($_product);
?>

In helper file I have following code ;

 namespace Test\ProductDetail\Helper;


 class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function DisplayDiscountLabel($_product)
{
   $originalPrice = $_product->getPrice();
    $finalPrice = $_product->getFinalPrice();

    $percentage = 0;
    if ($originalPrice > $finalPrice) {
        $percentage = number_format(($originalPrice - $finalPrice) * 100 / $originalPrice,0);
    }

    if ($percentage) {
        return "(".$percentage."%)";
    }

   }
}

but problem is that in phtml file I am not getting value of $_product?

Best Answer

In overrrided phtml file

../app/design/frontend/[VENDOR]/[THEME]/Magento_Catalog/templates/product/price/amount/default.phtml 

You can get product object in a variable $_product Using

<?php   $_product = $block->getSaleableItem(); ?>