How to Get Available Currency in Magento 2
Magento 2 stores support multiple currencies. The admin can set multi currency and default store currency under the store configuration. Sometimes, developers or store owners may require fetching the get available currency in Magento 2 for multiple purposes.
For an instance, suppose you want to show a drop-down list of all accepted currencies to the customers and want them to choose their preferred one. Thus, you may require to programmatically fetch the list of the allowed currency in Magento 2 and show to the customers. You can also learn to update currency rates automatically in Magento 2.
Also read: Magento 2 API – Get All Available Currencies
In this scenario, you can simply make use of the solution below to get the list of the available currencies in Magento 2 and use them as per your convenience.
Method to Get Available Currency in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<!--?php namespace Meetanshi\Paymulti\Model\Source; /** * Class Currency * @package Meetanshi\Paymulti\Model\Source */ class Currency extends \Magento\Config\Model\Config\Source\Locale\Currency { /** * @var \Magento\Framework\Locale\ListsInterface */ protected $_localeLists; /** * @var \Magento\Store\Model\StoreManagerInterface */ protected $_currencySymbol; /** * @var \Meetanshi\Paymulti\Helper\Data */ protected $_helper; /** * Currency constructor. * @param \Magento\Framework\Locale\ListsInterface $localeLists * @param \Magento\Store\Model\StoreManagerInterface $currencySymbol * @param \Meetanshi\Paymulti\Helper\Data $helper */ public function __construct( \Magento\Framework\Locale\ListsInterface $localeLists, \Magento\Store\Model\StoreManagerInterface $currencySymbol, \Meetanshi\Paymulti\Helper\Data $helper, \Magento\Directory\Block\Currency $currency ) { $this->_localeLists = $localeLists; $this->_currencySymbol = $currencySymbol; $this->_helper = $helper; $this->_currency = $currency; parent::__construct($localeLists); } /** * @return array * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function toOptionArray() { $_supportedCurrencyCodes = $this->_helper->getSupportedCurrency(); $_availableCurrencyCodes = $this->_currencySymbol->getStore()->getAvailableCurrencyCodes(true); if (!$this->_options) { $this->_options = $this->_localeLists->getOptionCurrencies(); } $options = []; foreach ($this->_options as $option) { if (in_array($option['value'], $_supportedCurrencyCodes) && in_array($option['value'], $_availableCurrencyCodes)) { $options[] = $option; } } return $options; } } |
In case you have any queries regarding the above solution or facing any issue while fetching the available currency in Magento 2, feel free to comment down below. I would be happy to assist you!
Also, do not forget to share the solution with the Magento community via social media.
Thank you!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Rising Trend of Subscription Model in eCommerce [2024]
How to Get Current URL in Magento 2
Next