Magento : How to Convert Price from Current Currency to Base Currency

If in some case you want to convert the price to base corrency in Magento then there is a very simple function available ‘currencyConvert()’ but this function is NOT WORKING in magento community 1.7 version.

<?php
$from 		= 'USD';
$to 		= 'NPR';
$price 		= 10;
$newPrice 	= Mage::helper('directory')->currencyConvert($price, $from, $to);
?>

Source

magento convert price in base currency
I tried another way to solve this. You can use the below working code to get the price in base currency.

<?php
//	Convert price to base currency from current currency
$totals 	= 	Mage::getSingleton('checkout/cart')->getQuote()->getTotals();
$subtotal 	= 	$totals["subtotal"]->getValue();
// Base Currency
$baseCurrencyCode 		= 	Mage::app()->getStore()->getBaseCurrencyCode();
// Current Currency
$currentCurrencyCode 	= 	Mage::app()->getStore()->getCurrentCurrencyCode();
if( $baseCurrencyCode != $currentCurrencyCode ){
	// Allowed currencies
	$allowedCurrencies 	= 	Mage::getModel('directory/currency')->getConfigAllowCurrencies();
	$rates 				= 	Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));
	$convertedSubtotal	=	$subtotal / $rates[$currentCurrencyCode];	
}else{
	$convertedSubtotal	=	$subtotal;
}
?>

Source

By Vaseem Ansari

I’m Vaseem, a Software Engineer by Profession, a Traveler, a Foodie by Heart and the founder of VaseemAnsari.com. I started traveling from college days and it has become a part of me now. So when ever I happen to get a chance I pack my bag and am on the roads. You can follow me on these social networks Facebook, Twitter, Google+ and Linkedin.

Leave a comment

Your email address will not be published. Required fields are marked *