How to get all the allowed currencies having currency rates.

Hello everyone, sometimes we need all those currencies that are allowed by admin and also have conversion rates set by the admin.

Following code will help you to achieve this.

First of all, In the Helper construct, add the following code,

public function __construct(
   \Magento\Directory\Block\Currency $currency,
   \Magento\Framework\App\Helper\Context $context
) {
   $this->_currency = $currency;
   parent::__construct($context);
}

after that, you can write this function to get the currencies codes with their corresponding names as below-

public function getCurrencies() //this will fetch the allowed currencies for which rates are set
{
    $currencies = $this->_currency->getCurrencies();
    $currencyData = [];
    foreach ($currencies as $code => $currencyName) {
        $currencyData[$code]['code'] = $code;
        $currencyData[$code]['name'] = $currencyName;
    }
    return $currencyData;
}

if you print $currencyData, it will display the result as you can see in the image-


This is all for now. Hope this will help.

For any queries, comment below.

. . .

Ask a Feature

css.php