25

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.

1
  • could you please help me . I have posted a new question (compare store currency & getCurrentCurrency) please check it on my profile . – abilash er Sep 26 '20 at 13:49
36

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()
4
  • 1
    How can I call default currency symbol in phtml page in my custom module? – Purushotam Sharma Aug 29 '17 at 15:06
  • This shows EUR instead of €... – Black Jun 23 '20 at 15:31
  • could you please help me . I have posted a new question (compare store currency & getCurrentCurrency) please check it on my profile . – abilash er Sep 26 '20 at 13:49
  • injecting the storemanager is not always possible, for example when you want to use it in other interfaces, in such cases using the object manager is an option – Fuseteam Oct 21 '20 at 13:30
6

This takes inspiration from Magento\Framework\Pricing\Render\Amount and it's working good in my case (behaving like Magento):

protected $_priceCurrency;

public function __construct(
  ...
  \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
  ...
)
{           
  $this->_priceCurrency = $priceCurrency;
  ...
}

/**
 * Get current currency code
 *
 * @return string
 */ 
public function getCurrentCurrencyCode()
{
  return $this->_priceCurrency->getCurrency()->getCurrencyCode();
}

You can get the currency symbol also:

/**
 * Get current currency symbol
 *
 * @return string
 */ 
public function getCurrentCurrencySymbol()
{
  return $this->_priceCurrency->getCurrency()->getCurrencySymbol();
}
0

expanding on the accepted answer if injecting the storemanagerinterface is not an option you can use the objectmanager like this:

/**
  5      * Get Store Name
  4      *
  3      * @return \Magento\Sales\Model\StoreManagerInterface
  2      */
  1     protected function getStore() {
123         $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  1         $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
  2         return $storeManager->getStore();
  3     }

after which the currency can get fetched with $this->getStore()->getCurrentCurrency()->getCode();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.