SEO Ultimate package released! Grab 20% off - SEO20 Explore

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. When you use Magento 2 platform to build your e-store, you can accept many currencies, that depends on the target customers. With this guide, not only default and current currency code but also the available currency code and allowed currency codes will be fetched clearly.

Overview of getting currency data in Magento 2

  • Step 1: Declare in Mageplaza_HelloWorld
  • Step 2: Get the output of the currency data in phtml file

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!



Next tutorial:

Module Development Series



Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
get
currency
data
currency-code
currency-rate
currency-symbol

Module Development

Comments for How to get currency data: code, rate, symbol in Magento 2

Please leave comments if you have any questions, feedbacks.

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

Shop By Brand

$99
3 reviews

People also searched for:

  • magento 2 get currency data
  • get currency data in magento 2
  • how to get currency data
  • how to get currency code currency rate currency symbol
  • how to get currency data in magento 2
  • how to get currency code currency rate currency symbol in magento 2