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

I use this code but it not work for me it gives error

Fatal error: Call to a member function getCode() on string in /home4/proddev/public_html/app/code/core/Mage/Directory/Model/Currency.php on line 194

2 Answers 2

1

Look at the function currentConvert() the price should be an float

replace $price=10; with $price=10.00;

/**
 * Convert currency
 *
 * @param float $amount
 * @param string $from
 * @param string $to
 * @return float
 */
public function currencyConvert($amount, $from, $to = null)
    {
        if (empty($this->_currencyCache[$from])) {
            $this->_currencyCache[$from] = Mage::getModel('directory/currency')->load($from);
        }
        if (is_null($to)) {
            $to = Mage::app()->getStore()->getCurrentCurrencyCode();
        }
        $converted = $this->_currencyCache[$from]->convert($amount, $to);
        return $converted;
    }

Edit:

Go to System>Configuration>Currency Setup and make sure the currencies you are using are selected in the Allowed Currencies setting.

Nb: Your error means that the convert method cannot find a rate !

3
  • still not work for me Commented Nov 16, 2017 at 11:47
  • look my updated answer @jigs parmar
    – PЯINCƎ
    Commented Nov 16, 2017 at 12:07
  • yes it is allowed Commented Nov 17, 2017 at 3:43
1

Make sure which is your base currency and to which currency you want to convert

Try this

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

Make sure you have allowed the currency from System > Configuration > Currency Setup and you have defined the currency rate in System > Manage Currency > Rates.

1
  • From USD to HKD is work but HKD to USD not work Commented Nov 17, 2017 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.