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!
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! 😃
Please feel free to mention your doubts, if any, in the above solution, in the Comments section below. I’d be happy to help 😊
Do not forget to rate the post with 5 stars! 🌟