Magento 2 Base Currency:
- The default currency set up in the backend.
- The admin uses it to set the product price in the backend.
- It can be set at global/website scope.
- It is applied for all store and store views.
Magento 2 Current Currency:
- It is used to display the price in the frontend.
- It is independent of the base currency.
- It can be set for each store view.
Given the difference between both the terms, let’s proceed with the method to convert price from current currency to base currency in Magento 2.
The store set up with multi-currencies might need this solution while the development process, in order to experiment with the features that require particular conditions for base currency!
Also Read, How to Auto Update Currency Rates in Magento 2
Solution to Convert Price From Current Currency to Base Currency in Magento 2:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $priceCurrencyFactory = $objectManager->get('Magento\Directory\Model\CurrencyFactory'); $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface'); $currencyCodeTo = $storeManager->getStore()->getCurrentCurrency()->getCode(); $currencyCodeFrom = $storeManager->getStore()->getBaseCurrency()->getCode(); $itemAmount = 100; // product price $rate = $priceCurrencyFactory->create()->load($currencyCodeTo)->getAnyRate($currencyCodeFrom); $itemAmount = $itemAmount * $rate;
That’s it!
Thank you.