Magento, Magento 2

How to Get Formatted Price with Currency in Magento 2

How to Get Formatted Price with Currency in Magento 2

In this tutorial, Today we will learn about how to get formatted price with currency in Magento 2. When we get product price but at that time price will return price without a currency symbol. So, you need to get product price with format by using this class Magento\Framework\Pricing\Helper\Data.

So, Let’s follow the steps that how to get product price with currency symbol in Magento 2. I added object manager and construct method. But, As per coding standard, I would recommend to use always construct method instead of object manager method.

You may also like this :

Using the Object Manager Method :

<?php
/**
 * Created By : Rohan Hapani
 */
$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
/**
 * Created By : Rohan Hapani
 */
namespace RH\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 and then, get product price with currency format. You can see that there is currency() function that is used to get a formatted price. In that function, you need to pass product’s final price as the first argument. The second argument is boolean for get product price in format or not and the last third argument is also boolean to get product price with the container or not.

I hope this blog will helpful for easily understand how to get the formatted price with currency in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Tagged , ,