How to Convert Price From Current Currency to Base Currency in Magento 2
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 2Solution to Convert Price From Current Currency to Base Currency in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 |
$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! 🌟
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.
2 Comments
This is working but the value is not exactly same as shown on the payment & review page in front of label “You will be charged for”
Hello Ishaq,
It should be working same
It seems like, you’ve mistaken something.
Please follow the above code properly
Thank You