2

I have below code

$currencyManager        = $_objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->getResource(); 
echo $currencySymbol    = $currencyManager->getCurrencyCodeFromToCurrency('USD');

It gives fatal error

Fatal error: Call to undefined method Magento\Directory\Model\ResourceModel\Currency::getCurrencyCodeFromToCurrency()

Can anybody guide me to get Currency Symbol from Currency Code?

4

It should be:

$currency = $_objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->load($currencyCode);
$currencySymbol = $currency->getCurrencySymbol();

It would be beter to use CurrencyRepository but it's not yet implemented in Magento 2.

| improve this answer | |
7

We can do this using Constructor injection.

/**
 * @var \Magento\Framework\Locale\CurrencyInterface
 */
protected $_localeCurrency

public function __construct(
    \Magento\Framework\Locale\CurrencyInterface $localeCurrency
)
{
    $this->_localecurrency = $localeCurrency;
}

Now you can use below code in your function to get currency symbol.

$this->_localecurrency->getCurrency($currencycode)->getSymbol();
| improve this answer | |
0

To get the symbol:

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_Symbol = $_objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->load($_code);
echo $_Symbol->getCurrencySymbol();
| improve this answer | |
  • Your code doesn't work. $_code is not defined. – J Plana Jan 25 '17 at 20:52
0

Instead of using object manager try with currency interface

public function __construct(
        \Magento\Framework\Locale\CurrencyInterface $localeCurrency
    )
{
    $this->localecurrency = $localeCurrency;
}
$this->localecurrency->getCurrency($currencycode)->getSymbol();

For full code please refer here http://webhintsexplorer.com/magento-2-get-currency-symbol-by-code/

| improve this answer | |
0

It also works like this:

$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());
| improve this answer | |

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.