Very often, we are required to get the prices of products, but we get those prices without a currency symbol. Therefore, we need to show prices according to the currency set in the store.
To show formatted prices with currency symbols, we are required to use the pricing helper class of Magento. We just need to inject the class Magento\Framework\Pricing\Helper\Data
inside PHTML or any type.
So, I have created PrcingService class inside my module SbDevBlog_Catalog.
How to get formatted Prices with Currency Symbol In Magento 2?
I have created PricingService.php inside SbDevBlog/Catalog/Services direcotry.
<?php
/**
* @copyright Copyright (c) sbdevblog (http://www.sbdevblog.com)
*/
namespace SbDevBlog\Catalog\Services;
use Magento\Framework\Pricing\Helper\Data as PricingHelper;
class PricingService
{
/**
* @var PricingHelper
*/
protected PricingHelper $priceHelper;
/**
* Constructor
*
* @param PricingHelper $priceHelper
*/
public function __construct(PricingHelper $priceHelper)
{
$this->priceHelper = $priceHelper;
}
/**
* Get Formatted Price
*
* @param float $price
*
* @return string
*/
public function getFormattedPrice(float $price):string
{
return $this->priceHelper->currency($price, true, false);
}
}
Alternatively, You can use
Magento\Framework\Pricing\PriceCurrencyInterface
to get formatted price.
<?php
/**
* @copyright Copyright (c) sbdevblog (http://www.sbdevblog.com)
*/
namespace SbDevBlog\Catalog\Services;
use Magento\Framework\Pricing\PriceCurrencyInterface;
class PricingService
{
/**
* @var PriceCurrencyInterface
*/
protected PriceCurrencyInterface $priceCurrency;
/**
* Constructor
*
* @param PricingHelper $priceHelper
*/
public function __construct(PricingHelper $priceCurrency)
{
$this->priceCurrency = $priceCurrency;
}
/**
* Get Formatted Price
*
* @param float $price
* @param bool $includeContainer
* @param int $precision
* @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope
* @param \Magento\Framework\Model\AbstractModel|string|null $currency
*
* @return string
*/
public function getFormattedPrice(float $price, bool $includeContainer, int $precision, $scope, $currency):string
{
return $this->priceCurrency->format($price,$includeContainer,$precision, $scope, $currency);
}
}
Let’s have look into the paremeters.
PARAMETERS | USE |
---|---|
price | Amount with Float Type to be formatted |
IncludeContainer | It is boolean type parameter. It will add the span tag with class=”price” if set to true. |
precision | It is int type paremeter. this tells the method about the number of digits to be considered after decimal. By default it is 2. |
Scope | It can be null, string, boolean,int or \Magento\Framework\App\ScopeInterface type. It tells the method about the number of digits to be considered after decimal |
currency | It is string type parameter. This is the currency code in which you want the amount to be formatted. It is used to symolise the formmated price |
That’s it. We must call the getFormattedPrice() function with a price parameter from the appropriate class or template file. It’s easy to show formatted Price With currency symbols in Magento2.
Checkout the post on updating the total using JS
Thanks for reading the post on How to get formatted Prices with Currency Symbol In Magento 2? Please share this article, and leave your comment and feedback. Please use a comment box to share your valuable feedback.
Download Source Code
Note: Please verify the code of this blog and the relevant git repository before using it in production.
🙂 HAPPY CODING 🙂