The PHP fatal error is stemming from a bug in the convert method where it assumes that $toCurrency is an object. The error, however, is being caused when it's trying to throw an exception:
throw new Exception(Mage::helper('directory')->__('Undefined rate from "%s-%s".', $this->getCode(), $toCurrency->getCode()));
The meaning of this error is that the convert
method cannot find a rate. I tested your code, and it works fine, as long as the rates exist for the currencies you are converting to/from.
What you'll need to do is navigate to System -> Configuration -> Currency Setup and make sure the currencies you are using are selected in the Allowed Currencies setting.
After you have selected the Allowed Currencies, you'll need to set the rates. You can do this by going to System -> Manage Currency Rates. From there you can either input your own conversion ratios into the grid or import them by clicking the Import button, then click Save Currency Rates.
Once you have the currency rates for the conversion setup, it should work without a hitch.
create just simple module and overrider default *Format.php** file,
app/code/Package/Modulename/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Framework\Locale\Format" type="Vendor\Currency\Model\Format" />
</config>
create model file,
app/code/Package/Modulename/Model/Format.php
<?php
namespace Package\Modulename\Model;
use Magento\Framework\Locale\Bundle\DataBundle;
class Format extends \Magento\Framework\Locale\Format
{
private static $defaultNumberSet = 'latn';
public function getPriceFormat($localeCode = null, $currencyCode = null)
{
$localeCode = $localeCode ?: $this->_localeResolver->getLocale();
if ($currencyCode) {
$currency = $this->currencyFactory->create()->load($currencyCode);
} else {
$currency = $this->_scopeResolver->getScope()->getCurrentCurrency();
}
$localeData = (new DataBundle())->get($localeCode);
$defaultSet = $localeData['NumberElements']['default'] ?: self::$defaultNumberSet;
$format = $localeData['NumberElements'][$defaultSet]['patterns']['currencyFormat']
?: ($localeData['NumberElements'][self::$defaultNumberSet]['patterns']['currencyFormat']
?: explode(';', $localeData['NumberPatterns'][1])[0]);
//your main changes are gone here.....
$decimalSymbol = '.';
$groupSymbol = ',';
$pos = strpos($format, ';');
if ($pos !== false) {
$format = substr($format, 0, $pos);
}
$format = preg_replace("/[^0\#\.,]/", "", $format);
$totalPrecision = 0;
$decimalPoint = strpos($format, '.');
if ($decimalPoint !== false) {
$totalPrecision = strlen($format) - (strrpos($format, '.') + 1);
} else {
$decimalPoint = strlen($format);
}
$requiredPrecision = $totalPrecision;
$t = substr($format, $decimalPoint);
$pos = strpos($t, '#');
if ($pos !== false) {
$requiredPrecision = strlen($t) - $pos - $totalPrecision;
}
if (strrpos($format, ',') !== false) {
$group = $decimalPoint - strrpos($format, ',') - 1;
} else {
$group = strrpos($format, '.');
}
$integerRequired = strpos($format, '.') - strpos($format, '0');
$result = [
//TODO: change interface
'pattern' => $currency->getOutputFormat(),
'precision' => $totalPrecision,
'requiredPrecision' => $requiredPrecision,
'decimalSymbol' => $decimalSymbol,
'groupSymbol' => $groupSymbol,
'groupLength' => $group,
'integerRequired' => $integerRequired,
];
return $result;
}
}
Thanks.
Best Answer