0

everybody!

In header on my store I need to display a symbol of current currency. In my custom block I inherited from \Magento\Directory\Block\Currency and added following code:

class Currency extends \Magento\Directory\Block\Currency
{
    /**
     * @return string
     */
    public function getCurrentCurrencySymbol()
    {
        return $this
            ->_currencyFactory->create()
            ->load($this->getCurrentCurrencyCode())
            ->getCurrencySymbol();
    }
}

This is work good for me, but when I added additional currency (Russian Ruble) in en_US locale function returned NULL.

I found temporary solution, but I not sure that this good idea.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$currencies = $objectManager
    ->create('\Magento\CurrencySymbol\Model\System\CurrencysymbolFactory')
    ->create()
    ->getCurrencySymbolsData();

return $currencies[$this->getCurrentCurrencyCode()]['displaySymbol'];

Tell me, please, how I can solve the issue.

Magento 2.1.7

2
  • You should not use objectManager directly in files. You should use dependency injection as Magento 2 coding standards. Jul 18, 2017 at 16:19
  • Ok, thank you. Where I can read about this? Jul 18, 2017 at 17:11

1 Answer 1

0

Try below code. Hope you will get your answer

    <?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(); 
    ?>
4
  • 1
    Abhishek, your code is the same as my first variant in the question Jul 18, 2017 at 15:15
  • Did you try my code? Jul 18, 2017 at 15:20
  • 1
    Yes, of course - it return null too. I added symbol tag in /vendor/magento/zendframework1/library/Zend/Locale/Data/en.xml file and it works, issue not happens. But it's right solution? Jul 18, 2017 at 16:01
  • But modifying the core library isn't good. In the further update it'll be lost Jul 26, 2018 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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