0

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?

2
  • Where you want to display the percentage in the page ? Do you want to be in a particular place?
    – St3phan
    Oct 5, 2017 at 9:42
  • @St3phan I want to display discount on product detail page where price is displayed.
    – jassi
    Oct 5, 2017 at 9:43

2 Answers 2

2

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(); ?>
0

Try below in phtml file

    $objectManager =  \Magento\Framework\App\ObjectManager::getInstance(); 
    $_product = $objectManager->get('Magento\Catalog\Model\Product')->load($_product->getId());
1
  • Getting errro - $_product->getId() -- undefined variable: _product in
    – jassi
    Oct 5, 2017 at 10:17

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.