Wednesday, November 18, 2015

Format price in the current locale and currency

Format price in the current locale and currency

To display price with currency symbol:

$finalPrice = 55;
Mage::helper('core')->currency($finalPrice, true, false);

// static mixed currency (float $value, [bool $format = true], [bool $includeContainer = true])


$finalPrice = 55;
$formattedPrice = Mage::helper('core')->formatPrice($finalPrice, false);

To display price without currency symbol:

$finalPrice = 55;
Mage::helper('core')->currency($finalPrice, false, false);


$finalPrice = 55;
Mage::getModel('directory/currency')->formatTxt($finalPrice, array('display' => Zend_Currency::NO_SYMBOL));

To get currency symbol and symbol name by currency code:

$currency_code = Mage::app()->getStore()->getCurrentCurrencyCode();
$currency_symbol = Mage::app()->getLocale()->currency( $currency_code )->getSymbol();
$currency_name = Mage::app()->getLocale()->currency( $currency_code)->getName();

To convert Price from Current Currency to Base Currency and vice-versa:

$baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$price = 100;
// convert price from current currency to base currency
$priceOne = Mage::helper('directory')->currencyConvert($price, $currentCurrencyCode, $baseCurrencyCode);
// convert price from base currency to current currency
$priceTwo = Mage::helper('directory')->currencyConvert($price, $baseCurrencyCode, $currentCurrencyCode);

To change price from any one currency to another:

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


No comments: