Magento-2 How to convert price from current currency to base currency
-
16-01-2021 - |
Question
I want to convert product price from the current currency to base currency.
For Example, if the product price is 5000 INR. I want to convert 5000 INR to USD.
I don't know which core function will be best used for. Can anyone give me examples? Any function which is used for convert price to current to base and base to current?
Purpose: I am adding a product to cart programmatically, if a current currency is USD then it's working properly but if I switch currency as INR, added product price convert INR amount to USD. So I want to convert INR amount to USD.
Solution
Solution:
public function convertPrice($amount = 0, $store = null, $currency = null)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$priceCurrencyObject = $objectManager->get('Magento\Framework\Pricing\PriceCurrencyInterface');
//instance of PriceCurrencyInterface
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
//instance of StoreManagerInterface
if ($store == null) {
$store = $storeManager->getStore()->getStoreId();
//get current store id if store id not get passed
}
$rate = $priceCurrencyObject->convert($amount, $store, $currency);
//it return price according to current store from base currency
//If you want it in base currency then use:
$rate = $this->_priceCurrency->convert($amount, $store) / $amount;
$amount = $amount / $rate;
return $amount;
}
Here, $amount
is the amount to which you want to convert.
$store
is the store id, from which store’s base currency you want to convert.
$currency
is the currency whom you want to convert, if you passed null then it takes current currency.
OTHER TIPS
You can use below code in your helper or in a function
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @var \Magento\Directory\Model\CurrencyFactory
*/
protected $currencyFactory;
/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Directory\Model\CurrencyFactory $currencyFactory
){
$this->storeManager = $storeManager;
$this->currencyFactory = $currencyFactory;
}
public function convertToBaseCurrency($price)
{
//you can also pass INR code here insted of below current store currency
$currentCurrency = $this->storeManager->getStore()->getCurrentCurrency()->getCode();
$baseCurrency = $this->storeManager->getStore()->getBaseCurrency()->getCode();
$rate = $this->currencyFactory->create()->load($currentCurrency)->getAnyRate($baseCurrency);
$returnValue = $price * $rate;
return $returnValue;
}
Magento itself provides all the convert configuration we only need to use it.
The simplest way to archive this!
class Price
{
protected $_priceHelper;
public function __construct(
\Magento\Framework\Pricing\Helper\Data $priceHelper,
) {
$this->_priceHelper = $priceHelper;
}
public function yourFunction(){
/* Your code */
$this->convertToBaseCurrency(100);
}
/**
* @param $price
* @return int|float
*/
public function convertToBaseCurrency($price)
{
$formattedCurrencyValue = $this->_priceHelper->currency($price, false, false);
return $formattedCurrencyValue;
}
}