How to get Currency data: Code, Rate, Symbol in Magento 2
Today I will show you how to get the currency data like code, rate, and symbol in Magento 2
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 symbol when 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 />';
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!
Explore Currency Formatter for Magento 2
Related Topics
- Magento 2 Tutorials
- Get parent products: Bundle, Grouped products in Magento 2
- Get List Products from Catalog Rule Condition
- Get Product Options in Magento 2
- Get Stock Items in Magento 2
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, let leave your words in the comment section below!
Enjoyed the tutorial? Spread it to your friends!
Sam Thomas
CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.
Featured Extensions
People also searched for
- magento 2 get currency
- magento 2 get currency symbol
- magento 2 get current currency
- magento 2 get current currency symbol
- magento 2 get store currency
- magento 2 get currency code
- magento 2 change currency programmatically
- get currency symbol magento 2
- magento 2 display price with currency symbol
- magento 2 currency symbol
- magento 2 currency converter api
- get currency magento 2
- get currency symbol in magento 2
- magento 2 get store currency symbol
- magento 2 currency convert programmatically
- magento 2 remove currency symbol from price
- magento 2 set currency programmatically
- magento 2 get product price with currency symbol
- 2.2.x, 2.3.x, 2.4.x