-
- Creepin

-
Total Posts: 27
Joined: 2012-03-28
|
OK, I got it to work. Just if someone else also wants to have this feature, here my approach:
At first some remarks:
Do not overwrite core code and create a module instead!
This only works fine if customers can only checkout in base currency! Otherwise there could be problems in admin panel. How to achieve this: http://www.magentocommerce.com/boards/viewthread/237750/
bugs: when selecting a configurable option, only the price in base currency changes, since there is no update method for the foreign currency yet.
Manual:
Make a Module and overwrite app/code/core/Mage/Directory/Model/Currency.php
My new class looks like that:
<?php
/** * Currency model * * @category Mage * @package Mage_Directory * @author Magento Core Team <core@magentocommerce.com> */
class MyCode_TwoCurrencies_Model_Currency extends Mage_Directory_Model_Currency { /** * Convert price to currency format * * @param double $price * @param string $toCurrency * @return double */
// public function convert($price, $toCurrency=null) //never changes the currency, so displayed is always basecurrency public function convert($price, $toCurrency=null, $staybase=true) { if (is_null($toCurrency) || $staybase) { return $price; } elseif ($rate = $this->getRate($toCurrency)) { return $price*$rate; }
throw new Exception(Mage::helper('directory')->__('Undefined rate from "%s-%s".', $this->getCode(), $toCurrency->getCode())); }
/** * Apply currency format to number with specific rounding precision * * @param float $price * @param int $precision * @param array $options * @param bool $includeContainer * @param bool $addBrackets * @return string */ public function formatPrecision($price, $precision, $options=array(), $includeContainer = true, $addBrackets = false) { if (!isset($options['precision'])) { $options['precision'] = $precision; }
//get current currency. if is base currency use standard return $currencycode = Mage::app()->getStore()->getCurrentCurrencyCode(); $basecode = Mage::app()->getStore()->getBaseCurrencyCode();
if ($includeContainer) { if ($currencycode==$basecode){ return '<span class="price">' . ($addBrackets ? '[' : '') . $this->formatTxt($price, $options) . ($addBrackets ? ']' : '') . '</span>'; //if not return base currency and in brackets the foreign currency ($currprice) } else { $currencyrate = Mage::app()->getStore()->getCurrentCurrencyRate(); //calculat price in foreign currency $currprice=$price*$currencyrate; $addBrackets=true; return '<span class="price">'.$this->formatTxt($price, $options,true).'</span> <span class="bprice">' . ($addBrackets ? '[' : '') . $this->formatTxt($currprice, $options) . ($addBrackets ? ']' : '') . '</span>';
} } if ($currencycode==$basecode){ return $this->formatTxt($price, $options); } else { $currencyrate = Mage::app()->getStore()->getCurrentCurrencyRate(); $currprice=$price*$currencyrate; $addBrackets=true; return '<span class="price">'.$this->formatTxt($price, $options,true).'</span> <span class="bprice">' . ($addBrackets ? '[' : '') . $this->formatTxt($currprice, $options) . ($addBrackets ? ']' : '') . '</span>'; } } // public function formatTxt($price, $options=array()) //formats the price in base format or in current currency format depending in $base public function formatTxt($price, $options=array(),$base=false) { if (!is_numeric($price)) { $price = Mage::app()->getLocale()->getNumber($price); } $basecode = Mage::app()->getStore()->getBaseCurrencyCode(); $code = ($base) ? $basecode : $this->getCode(); /** * Fix problem with 12 000 000, 1 200 000 * * %f - the argument is treated as a float, and presented as a floating-point number (locale aware). * %F - the argument is treated as a float, and presented as a floating-point number (non-locale aware). */ $price = sprintf("%F", $price); return Mage::app()->getLocale()->currency($code)->toCurrency($price, $options); }
//is important for configurable products! this price is formatted by javascript and uses this function for the pattern. public function getOutputFormat() { $formated = $this->formatTxt(0,array(),true); $number = $this->formatTxt(0, array('display'=>Zend_Currency::NO_SYMBOL)); return str_replace($number, '%s', $formated); }
}
|