How to get formatted custom price
-
04-03-2021 - |
Question
I have set my product custom price in an observer..
$item->setOriginalCustomPrice(9.99);
$item->setCustomPrice(9.99);
I can output the price value correclty like so:
echo $item->getOriginalPrice();
// 9.99
But when I try to output it formatted, it gives me the original price.
$finalPrice = $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue();
// £19.99
I tried
$finalPrice = $product->getPriceInfo()->getPrice('custom_price')->getAmount()->getValue();
But that gives an error.
Solution
Use this code for price with currency to display.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data');
$finalPrice = $priceHelper->currency($product->getCustomPrice(), true, false);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange