Sometimes during development or customization in Magento 2, you may have some cases where you need to display a formatted price with currency.
Magento 2, out of the box comes with Magento\Framework\Pricing\Helper\Data and Magento\Framework\Pricing\PriceCurrencyInterface classes which you can use to get formatted price with currency.
In this article, we will show you how to Get Formatted Price With Currency in Magento 2
If you are looking to format price using JavaScript in Magento 2 check here
1. Using "Magento\Framework\Pricing\Helper\Data"
This \Magento\Framework\Pricing\Helper\Data class in Magento2 includes:
- currency($value, $format = true, $includeContainer = true) method to get formatted price with curency for current store
- currencyByStore($value, $store = null, $format = true, $includeContainer = true) method to get formated price with currency for specifc store.
Here is the description of each parameter:
- $value: your price value, a float parameter
- $store: your store id, an int parameter
- $format: format or not, is a bool parameter
- $includeContainer: use price HTML or not, is a bool parameter
You can use \Magento\Framework\Pricing\Helper\Data in your custom class to get a formatted price with currency for the current or specific store.
a) Using Model
Create your custom model Vendor\Module\Model\MyConfig where you retrieve price format with currency and use it further when needed in your code.
<?php
namespace Vendor\Module\Model;
use Magento\Framework\Pricing\Helper\Data as PricingHelper;
class MyModel
{
/**
* @var PricingHelper
*/
protected $pricingHelper;
/**
* @param PricingHelper $pricingHelper
*/
public function __construct(
PricingHelper $pricingHelper
) {
$this->pricingHelper = $pricingHelper;
}
/**
* Convert and format price value for current store
*
* @param float $price
* @return float|string
*/
public function getFormattedPrice($price)
{
return $this->pricingHelper->currency($price, true, false);
}
/**
* Convert and format price value for specified store
*
* @param float $price
* @param int|\Magento\Store\Model\Store $store
* @return float|string
*/
public function getFormattedPriceByStore($price, $store)
{
return $this->pricingHelper->currencyByStore($price, $store, true, false);
}
}
b) Using Block
You can inject your custom model Vendor\Module\Model\MyModel in your block to retrieve formatted prices and other details.
<?php
namespace Vendor\Module1\Block;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Vendor\Module\Model\MyModel;
class MyBlock extends Template
{
/**
* @var MyModel
*/
protected $myModel;
/**
* @param Context $context
* @param MyModel $myModel
* @param array $data
*/
public function __construct(
Context $context,
MyModel $myModel,
array $data = []
) {
$this->myModel = $myModel;
parent::__construct($context, $data);
}
/**
* Get formatted price for current store
*
* @param float $price
* @return float|string
*/
public function getFormattedPrice($price)
{
return $this->myModel->getFormattedPrice($price);
}
/**
* Get formatted price for specific store
*
* @param float $price
* @param int|\Magento\Store\Model\Store $store
* @return float|string
*/
public function getFormattedPriceByStore($price, $store)
{
return $this->myModel->getFormattedPriceByStore($price, $store);
}
}
a) Using PHTML
In the phtml template file, you can get the formatted price with currency from your custom block Vendor\Module\Block\MyBlock
<?php
$price = 'YOUR PRICE' // eg. 300;
$store = 'YOUR STORE ID' // eg. 1;
?>
<?= $block->escapeHtml($block->getFormattedPrice($price)) ?>
<?= $block->escapeHtml($block->getFormattedPriceByStore($price, $store)) ?>
2. Using "PriceCurrencyInterface"
This \Magento\Framework\Pricing\PriceCurrencyInterface class in Magento 2 includes these methods:
- convert($amount, $scope = null, $currency = null) to convert and round price value
- convertAndRound($amount, $scope = null, $currency = null, $precision = self::DEFAULT_PRECISION) to convert and round price value
- format($amount, $includeContainer = true, $precision = self::DEFAULT_PRECISION, $scope = null, $currency = null) to format price value
- convertAndFormat($amount, $includeContainer = true, $precision = self::DEFAULT_PRECISION, $scope = null, $currency = null) to convert and format price value
- round($price) to round price
- getCurrency($scope = null, $currency = null) to get currency model
- getCurrencySymbol($scope = null, $currency = null) to get currency symbol
Here is the description of each parameter:
- $amount: the amount value, a float paramter
- $scope: the scope value, can be null|string|bool|int|\Magento\Framework\App\ScopeInterface
- $currency: the currency value, can be \Magento\Framework\Model\AbstractModel|string|null
- $precision: the precision value, can be intiger
- $includeContainer: use price html or not, a bool paramater
- $price: the price value, a float paramter
You can use \Magento\Framework\Pricing\PriceCurrencyInterface in your custom class to get a formatted price with currency.
a) Using Model
Create your custom model Vendor\Module\Model\MyConfig where you retrieve price format with currency and use it further when needed in your code.
<?php
namespace Vendor\Module\Model;
use Magento\Framework\Pricing\PriceCurrencyInterface;
class MyModel
{
/**
* @var PriceCurrencyInterface
*/
protected $pricingInterface;
/**
* @param PriceCurrencyInterface $pricingInterface
*/
public function __construct(
PriceCurrencyInterface $pricingInterface
) {
$this->pricingInterface = $pricingInterface;
}
/**
* Get formatted price with currency for current store
*
* @param float $price
* @return float
*/
public function getFormattedPrice($price)
{
return $this->pricingInterface->format($price,true,2);
}
/**
* Get currency Symbol of current store
*
* @return string
*/
public function getCurrencySymbol()
{
return $this->pricingInterface->getCurrencySymbol();
}
/**
* Get rounded price
*
* @deprecated 102.0.1
* @param float $price
* @return float
*/
public function getRoundedPrice($price)
{
return $this->pricingInterface->round($price);
}
}
a) Using Block
You can inject your custom model Vendor\Module\Model\MyModel in your block to get formatted prices and other details.
<?php
namespace Vendor\Module\Block;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Vendor\Module\Model\MyModel;
class MyBlock extends Template
{
/**
* @var MyModel
*/
protected $myModel;
/**
* @param Context $context
* @param MyModel $myModel
* @param array $data
*/
public function __construct(
Context $context,
MyModel $myModel,
array $data = []
) {
$this->myModel = $myModel;
parent::__construct($context, $data);
}
/**
* Get formatted price with currency for current store
*
* @param float $price
* @return float
*/
public function getFormattedPrice($price)
{
return $this->myModel->getFormattedPrice($price);
}
/**
* Get currency Symbol for current store
*
* @return string
*/
public function getCurrencySymbol()
{
return $this->myModel->getCurrencySymbol();
}
/**
* Get rounded price
*
* @deprecated 102.0.1
* @param float $price
* @return float
*/
public function getRoundedPrice($price)
{
return $this->myModel->getRoundedPrice($price);
}
}
a) Using PHTML
In the phtml template file, you can get the formatted price with currency from your custom block Vendor\Module\Block\MyBlock
Example:
<?php
$price = 'YOUR PRICE' // eg. 300;
?>
<?= $block->escapeHtml($block->getFormattedPrice($price)) // eg. $300.00 ?>
<?= $block->escapeHtml($block->getCurrencySymbol()) // eg. $ ?>
<?= $block->escapeHtml($block->getRoundedPrice($price)) // eg.300 ?>
Conclusion
We hope you find this guide to "Get Formatted Price With Currency in Magento 2" helpful. Feel free to share or leave a comment below. Your opinion is much appreciated!
If you're looking for experienced Magento 2 developers to customize your site, fix issues, develop custom solutions, maintain, or anything else, feel free to check on this collection of Quality Services For Magento 2, choose the service that best suits your needs, or contact us
Comment(s)