5 (100%) 1 vote

RELATED:

In core magento, it has rates from base currency to other currencies. It is quite easy to convert currency from base. However, sometime if you want to convert currency from current currency to base or other currencies. Core magento don’t support this function. To day, we will try to to do it.

1 Convert currency from base to other currency 2 Convert currency from another currency to base currency 3 Convert currency from a currency to a currency

1.Convert currency from base to other currency:

$priceConverted = $this->_storeManager->getStore()->getBaseCurrency()->convert($price ,$toCurrency); With $this->_storeManager is Magento\Store\Model\StoreManagerInterface 1 2 3 4 5 $ priceConverted = $ this -> _storeManager -> getStore ( ) -> getBaseCurrency ( ) -> convert ( $ price , $ toCurrency ) ; With $ this -> _storeManager is Magento \ Store \ Model \ StoreManagerInterface

2. Convert currency from another currency to base currency:

$rateToBase = $this->_currencyFactory->create()->load($fromCurrency)->getAnyRate($this->_storeManager->getStore()->getBaseCurrency()->getCode()); $priceConverted = $price * $rateToBase; With $this->_currencyFactory is \Magento\Directory\Model\CurrencyFactory 1 2 3 4 5 $ rateToBase = $ this -> _currencyFactory -> create ( ) -> load ( $ fromCurrency ) -> getAnyRate ( $ this -> _storeManager -> getStore ( ) -> getBaseCurrency ( ) -> getCode ( ) ) ; $ priceConverted = $ price * $ rateToBase ; With $ this -> _currencyFactory is \ Magento \ Directory \ Model \ CurrencyFactory

3.Convert currency from a currency to a currency: