How to get Currency data: Code, Rate, Symbol in Magento 2
Vinh Jacker | 12-18-2024
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!
Related Topics
- 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!