Hello Magento Folks?,
How are you all working? I hope all are safe and healthy. Welcome to Magento Tutorial blog where we will learn How To Get Formatted Price With Currency In Magento 2. Also, check our latest and interesting article published where I have explained Why 75% of Your Marketing Emails are Never Opened and What to Do About It. Let’s Dive In?.
Contents
Magento 2 provides many methods to implement the correct formatting of prices or any type of value. In Magento 2 you can achieve formatted price with currency by using the object manager. One can easily convert a simple number into currency by implementing any of the given below solutions.
Create one block file on our custom extension add Customprice.php in the following path
app\code\Vendor\Extension\Block\Customprice
<?php namespace Vendor\Extension\Block; class Customprice extends \Magento\Framework\View\Element\Template { protected $priceCurrency; public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, array $data = [] ) { $this->priceCurrency = $priceCurrency; parent::__construct($context, $data); } public function getCurrencyWithFormat($price) { return $this->priceCurrency->format($price,true,2); } public function getRoundedPrice($price) { return $this->priceCurrency->round($price); } public function getCurrentCurrencySymbol() { return $this->priceCurrency->getCurrencySymbol(); } }
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $priceHelper = $objectManager->create('Magento\Framework\Pricing\PriceCurrencyInterface'); $price = 100; echo $priceHelper->convertAndFormat($price); //₹100.00 echo $priceHelper->round($price); // 100 echo $priceHelper->getCurrencySymbol(); //₹ ?>
Note: Using ObjectManager for Magento development is not recommended by us and Magento. This is just for Knowledge purposes.
Hence, you can choose any of the above methods to implement in your store if you require to format currency in Magento 2. In case any casualties faced raising the queries in the comment section below. If the article is helpful then share it with your Developer friend.
Happy Reading?
In modern web development, seamless navigation and state management are crucial for delivering a smooth…
Magento Open Source 2.4.8 beta version released on October 8, 2024. The latest release of…
Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…
As the world of eCommerce continues to thrive, Shopify has become one of the most…
Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…
Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…