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?

5 Answers 5

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.

0
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();
0

To get the symbol:

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_Symbol = $_objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->load($_code);
echo $_Symbol->getCurrencySymbol();
1
  • Your code doesn't work. $_code is not defined.
    – J Plana
    Commented Jan 25, 2017 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/

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());

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.