How to get Currency data: Code, Rate, Symbol in Magento 2
Vinh Jacker | 03-17-2025
Today I will show you how to get the currency data like code, rate, and symbol in Magento 2. When you use Magento 2 platform to build your e-store, you can accept many currencies, which depends on the target customers. With this guide, we will be fetching all base, default, and current currency codes clearly.
Overview Magento 2 get currency data
Step 1: Declare in Mageplaza_HelloWorld
You will use a block class of the module Mageplaza_HelloWorld
, then possibly inject the object of StoreManagerInterface & Currency
class in the constructor of the module’s block class.
app/code/Mageplaza/HelloWorld/Block/HelloWorld.php
<?php
namespace Chapagain\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
protected $_storeManager;
protected $_currency;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Directory\Model\Currency $currency,
array $data = []
)
{
$this->_storeManager = $storeManager;
$this->_currency = $currency;
parent::__construct($context, $data);
}
/**
* Get store base currency code
*
* @return string
*/
public function getBaseCurrencyCode()
{
return $this->_storeManager->getStore()->getBaseCurrencyCode();
}
/**
* Get current store currency code
*
* @return string
*/
public function getCurrentCurrencyCode()
{
return $this->_storeManager->getStore()->getCurrentCurrencyCode();
}
/**
* Get default store currency code
*
* @return string
*/
public function getDefaultCurrencyCode()
{
return $this->_storeManager->getStore()->getDefaultCurrencyCode();
}
/**
* Get allowed store currency codes
*
* If base currency is not allowed in current website config scope,
* then it can be disabled with $skipBaseNotAllowed
*
* @param bool $skipBaseNotAllowed
* @return array
*/
public function getAvailableCurrencyCodes($skipBaseNotAllowed = false)
{
return $this->_storeManager->getStore()->getAvailableCurrencyCodes($skipBaseNotAllowed);
}
/**
* Get array of installed currencies for the scope
*
* @return array
*/
public function getAllowedCurrencies()
{
return $this->_storeManager->getStore()->getAllowedCurrencies();
}
/**
* Get current currency rate
*
* @return float
*/
public function getCurrentCurrencyRate()
{
return $this->_storeManager->getStore()->getCurrentCurrencyRate();
}
/**
* Get currency symbol for current locale and currency code
*
* @return string
*/
public function getCurrentCurrencySymbol()
{
return $this->_currency->getCurrencySymbol();
}
}
?>
You can see more functions in vendor/magento/module-store/Model/Store.php
and vendor/magento/module-directory/Model/Currency.php
.
Step 2: Get the output of the currency data in phtml
file
Allow getting and printing the currency rate, currency code, and currency symbolwhen you run the below command in the template phtml
file.
echo $block->getCurrentCurrencySymbol() . '<br />';
echo $block->getCurrentCurrencyCode() . '<br />';
echo $block->getBaseCurrencyCode() . '<br />';
echo $block->getDefaultCurrencyCode() . '<br />';
echo $block->getCurrentCurrencyRate() . '<br />';
print_r($block->getAvailableCurrencyCodes()) . '<br />';
print_r($block->getAllowedCurrencies()) . '<br />';
Final words
This is the end, I hope it is helpful for you. In case that you have any queries about the article or any questions in general, use the comment section below!
If you’re planning to display this currency data on the frontend, especially across multiple store views or locales, you might want to format it properly for each customer. The Magento 2 Currency Format extension helps you control how currency codes, symbols, and decimal places appear across your store — making the final output cleaner and easier to read.
Related Topics