Magento – Currency.php: Fatal error: Call to a member function getCode() on a non-object

currencyfatal errormagento-1.8

I use Magento ver. 1.8.1.0 and when I go one my website I've got this error:

Fatal error: Call to a member function getCode() on a non-object in app/code/core/Mage/Directory/Model/Currency.php on line 193

When I choose USD for currency in Configuration -> Currency setting, it's ok, but after, with my payment method it doesn't work because it need CHF (Swiss franc) so if I choose CHF for Currency I have the Fatal error for all the website…

On line 194 I have this function:

public function convert($price, $toCurrency = null)
{
    if (is_null($toCurrency)) {
        return $price;
    } else {
        $rate = $this->getRate($toCurrency);
        if ($rate) {
            return $price * $rate;
        }
    }

    throw new Exception(Mage::helper('directory')->__('Undefined rate from "%s-%s".', $this->getCode(),$toCurrency->getCode()));
}

and the getCode() function:

/**
 * Get currency code
 *
 * @return string
 */
public function getCode()
{
    return $this->_getData('currency_code');
}

I don't know what is wrong, somebody can help me?
Thank you

Best Answer

I think the currency is missing in the directory_currency_rate table.

Check the table for your currency. If it is missing, you have two options to solve it.

  1. Enable the cronjob to retrieve the currency from the webservice in configuration.
  2. Go to "System > Manage Currency > Rates and click the import button

You should clear the cache and delete all your cookies (to get rid of your session and shopping cart) afterwards and test again.

Related Topic