Magento 2 – How to Get Current Currency Code

blockscurrencymagento2

In Magento 1, you could retrieve the current currency code by doing:

Mage::app()->getStore()->getCurrentCurrencyCode()

I'm wondering what is the recommended way of doing it in Magento 2. In my case in a block.

Best Answer

In a block

In Magento 2, you can use the \Magento\Store\Model\StoreManagerInterface which is stored in an accessible variable $_storeManager for every class extending \Magento\Framework\View\Element\Template so most of the block classes (Template, Messages, Redirect block types but not Text nor TextList).

This way in your block, you can directly type the following code to get the current currency code:

$this->_storeManager->getStore()->getCurrentCurrency()->getCode()

No need to inject the \Magento\Store\Model\StoreManagerInterface in your construct as it's a variable accessible from any block class.

In any other class

You can inject the \Magento\Store\Model\StoreManagerInterface in your constructor:

protected $_storeManager;

public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
{
    $this->_storeManager = $storeManager;
}

Then call the same function as the block:

$this->_storeManager->getStore()->getCurrentCurrency()->getCode()
Related Topic