Steps to Get Formatted Price with Currency in Magento 2?

Today we will understand the process to get formatted price with currency in Magento 2. Formatted price is required when you are delivering in places with different currencies. So, you need to get product price with format with this class Magento\Framework\Pricing\Helper\Data.

Let’s follow the steps to know how to get product price with currency symbol in Magento 2. In this blog, we've added two methods, .i.e. object manager and construct method. But, As per coding standard, we'd like to recommend to use always construct method instead of object manager method.

Using Object Manager

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); // Current Product Object
$priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data'); // Instance of Pricing Helper echo $priceHelper->currency($product->getFinalPrice(), true, false); 
?>

Using the Construct Method

<?php
namespace MD\Helloworld\Block;
class Helloworld extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Magento\Framework\Registry
     */
    protected $registry;

    /**
     * @var \Magento\Framework\Pricing\Helper\Data
     */
    protected $priceHelper;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Framework\Registry                      $registry
     * @param \Magento\Framework\Pricing\Helper\Data           $priceHelper
     * @param array                                            $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Pricing\Helper\Data $priceHelper,
        array $data = []
    ) {
        $this->registry = $registry;
        $this->priceHelper = $priceHelper;
        parent::__construct($context, $data);
    }

    public function getProductPrice()
    {
        $product = $this->registry->registry('current_product');
        return $this->priceHelper->currency($product->getFinalPrice(), true, false);
    }
}

You can use both methods in your module to get product price with currency format. You might have noticed that there is a currency() function that is used to get a formatted price. In that function, you need to pass the product’s final price as the first argument. The second argument is boolean forget product price in format or not and the last third argument is also boolean to get product price with the container or not.

We hope this blog proved to be helpful and easily understandable. If we missed out anything feel free to reach us out. We'd be happy to help.

Also read: How to Set Up Currency in Magento 2 Store?