0

I want to get current currency symbol in header, but it is not displaying anthing.

I am using below code:

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterfa‌​ce'); 
$currencyCode = $storeManager->getStore()->getCurrentCurrencyCode(); 
$currency = $objectManager->create('Magento\Directory\Model\CurrencyFact‌​ory')->create()->loa‌​d($currencyCode); 
echo $currencySymbol = $currency->getCurrencySymbol(); ?> 
0

3 Answers 3

3

You can used below code in file block, and called that block function in header :

protected $_priceCurrency;

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

public function getCurrentCurrencySymbol()
{
  return $this->_priceCurrency->getCurrency()->getCurrencySymbol();
}

You need to call getCurrentCurrencySymbol() in header file.

0
1

I have got answer:

  <?php 
        $objectManager =  \Magento\Framework\App\ObjectManager::getInstance(); 
        $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
        $currency = $objectManager->get('\Magento\Directory\Model\Currency');

        $store = $storeManager->getStore();


        $currencyCode = $store->getCurrentCurrencyCode();

        $currentcurrency = trim($currencyCode);
        $currency = $objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->load($currentcurrency);
        $currencySymbol = $currency->getCurrencySymbol(); 
         echo $currencySymbol;
?> 
1
  • There is also a short way : $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); $currency = $objectManager->get('\Magento\Directory\Model\Currency'); $store = $storeManager->getStore(); print_r($store->getWebsite()->getBaseCurrency()->getCurrencySymbol()); Apr 19, 2018 at 11:57
0

Here i am showing u how to get the currency symbol from block file

    <?php

    namespace CompanyName\ModuleName\Block\Index;

    class Index extends \Magento\Framework\View\Element\Template {

    protected $_currency;

    public function __construct(\Magento\Catalog\Block\Product\Context $context,
    \Magento\Directory\Model\Currency $currency,
    array $data = []) {

        parent::__construct($context, $data);

    }


    protected function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getCurrentCurrencySymbol()
    {
        return $this->_currency->getCurrencySymbol();
    }   

}

in the corresponding template use echo $block->getCurrentCurrencySymbol();

1
  • you are welcome @ vinod kumar Jun 22, 2017 at 13:20

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