Try the Demo

Magento Forum

   
Foreign currency besides base currency just as info
 
Creepin
Jr. Member
 
Total Posts:  27
Joined:  2012-03-28
 

Hey there!

I want to show the selected currency additionally to the base currency, in brackets or something similar. The selected currency should just be an information to the customer, since he will pay in the base currency in every case.

I searched the internet but didn’t find a solution.

Does anyone has an approach, link or code snippet for me?

Thanks in advance!

Best regards,
Horst

 
Magento Community Magento Community
Magento Community
Magento Community
 
Creepin
Jr. Member
 
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);
    
}

   
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
Creepin
Jr. Member
 
Total Posts:  27
Joined:  2012-03-28
 

making it work with configurable products you have to adjust Configurable.php and javascript. I am using SCP so my explanation refers to SCP.

in app\code\community\OrganicInternet\SimpleConfigurableProducts\Catalog\Block\Product\View\Type\Configurable.php:
after

if (Mage::getStoreConfig('SCP_options/product_page/change_attributes')) {
                $childBlock 
$this->getLayout()->createBlock('catalog/product_view_attributes');
                
$childProducts[$productId]["productAttributes"$childBlock->setTemplate('catalog/product/view/attributes.phtml')
                    ->
setProduct($product)
                    ->
toHtml();
            
}

add

$childProducts[$productId]["ffinalprice""[".$this->getCurrentStore()->getCurrentCurrency()->formatTxt($product->getFinalPrice() * Mage::app()->getStore()->getCurrentCurrencyRate())."]";

and after

$config['shortDescription'$this->helper('catalog/output')->productAttribute($pnl2br($p->getShortDescription()), 'short_description');

add

$config["ffinalprice""[".$this->getCurrentStore()->getCurrentCurrency()->formatTxt($p->getFinalPrice() * Mage::app()->getStore()->getCurrentCurrencyRate())."]";

in skin\frontend\base\default\js\scp_product_extension.js:

add

this.updateForeignCurrency(childProductId);

within

if(childProductId){

and add

this.updateForeignCurrency(false);

in the following else statement.

add

Product.Config.prototype.updateForeignCurrency = function(productId{
    
var ffprice this.config.ffinalprice;
    if (
productId && this.config.childProducts[productId].ffinalprice{
        ffprice 
this.config.childProducts[productId].ffinalprice;
    
}
    
$$('.bprice').each(function(el{
        el
.innerHTML ffprice;
    
});
};

before

Product.Config.prototype.updateProductImage = function(productId{

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top