How to get Formatted Price with Currency in Magento 2

Hello Everyone,

In this blog, we will learn about how to get Formatted Price with Currency in Magento 2.

Convert a simple number into Format Price with Currency Symbol.

Without wasting your time, let us guide you straight away. Follow the easy step given below to Create Customer Attribute in Magento 2.

STEPS FOR GET FORMATTED PRICE WITH CURRENCY IN MAGENTO 2

They are two methods to get Formatted Price with Currency in Magento 2.

  1. Using Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get(‘Magento\Framework\Registry’)->registry(‘current_product’); $priceHelper = $objectManager->create(‘Magento\Framework\Pricing\Helper\Data’); 

We do not recommend using Object Manager directly.

  1. Using Construct Method:
namespace Vendor\Extension\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);

     }

}

Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can get Formatted Price with Currency in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog?

Stay tuned with us on our site to get new updates of Magento.

Thanks for reading and visiting our site.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *