How to Get Currency Code, Currency Symbol & Currency Rate in Magento 2

In this article, I am going to show you how we can get the store’s currency code, currency symbol, and currency rate in Magento 2. In the below example, I will be using both Dependency Injection (DI) and Object Manager to get the store currency information.

Using Dependency Injection (DI)

Here is the example code to get the all base, default and current currency code in Magento 2 using dependency injection. In the below code, We will also be fetching available currency codes and allowed currency codes.

In this, we might need to inject the object of Magento\Store\Model\StoreManagerInterface & Magento\Directory\Model\Currency class in the constructor of our module’s block class and access it from the view ( .phtml ) file.

Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php

<?php
namespace YourCompanyName\YourModuleName\Block;

class YourCustomBlock extends \Magento\Framework\View\Element\Template
{ 
    protected $_storeManager;
    protected $_currency;

    public function __construct(
        \Magento\Framework\View\Element\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();
    } 
}

See more functions in vendor/magento/module-store/Model/Store.php and vendor/magento/module-directory/Model/Currency.php.

Now, we can use the functions in our view (.phtml) file as follows,

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 />';

Using Object Manager

Here is the example code to get the currency code, currency symbol and currency rate in Magento 2 using object manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$currency = $objectManager->get('\Magento\Directory\Model\Currency');

$store = $storeManager->getStore();

echo $currency->getCurrencySymbol() . '<br />';
echo $store->getCurrentCurrencyCode() . '<br />';
echo $store->getBaseCurrencyCode() . '<br />';
echo $store->getDefaultCurrencyCode() . '<br />';
echo $store->getCurrentCurrencyRate() . '<br />';

print_r($store->getAvailableCurrencyCodes()) . '<br />';
print_r($store->getAllowedCurrencies()) . '<br />';

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *