1
votes

I have a magento store with multiple currencies. The product prices are displayed in the relevant currencies according to the IP addresses. I want to set up GBP as the default currency for US customers, i.e. instead of USD (as per the ip address) the prices should be displayed in GBP for US clients.

I am new to magento, any help will be appreciated.

1

1 Answers

0
votes

i have create an for this works,you can try this...

Step1:Create config.xml under app/code/local/Amit/AutoCurrency/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Amit_AutoCurrency>
            <version>1.0.0</version>
        </Amit_AutoCurrency>
    </modules>
    <global>
        <models>            
            <core>
                <rewrite>
                    <store>Amit_AutoCurrency_Model_Store</store>
                </rewrite>
            </core>
        </models>        
        <helpers>
            <autocurrency>
                <class>Amit_AutoCurrency_Helper</class>
            </autocurrency>
        </helpers>      
    </global>
</config>

In store.php is app/code/local/Amit/AutoCurrency/Model/Store.php

<?php

class Amit_AutoCurrency_Model_Store extends Mage_Core_Model_Store
{       
    /**
     * Update default store currency code
     *
     * @return string
     */
    public function getDefaultCurrencyCode()
    {
        $result = $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_DEFAULT);       

        return $this->getCurrencyCodeByIp($result);     
    }

    /**
     * Get Currency code by IP Address
     *
     * @return string
     */
    public function getCurrencyCodeByIp($result = '') 
    {
        // load GeoIP binary data file
        $geoIp = Mage::helper('autocurrency')->loadGeoIp();

        $ipAddress = Mage::helper('autocurrency')->getIpAddress();

        // get country code from ip address
        $countryCode = geoip_country_code_by_addr($geoIp, $ipAddress);

        if($countryCode == '') {
            return $result;
        }

        // get currency code from country code
        $currencyCode = geoip_currency_code_by_country_code($geoIp, $countryCode);

        // close the geo database  
        geoip_close($geoIp);    

        // if currencyCode is not present in allowedCurrencies
        // then return the default currency code
        $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();      
        if(!in_array($currencyCode, $allowedCurrencies)) {
                if($currencyCode="USD"){
                return "GBP";
                }

            return $result;
        }
        if($currencyCode="USD"){
        return "GBP";
        }


        return $currencyCode;
    }
}

And help function is app/code/local/Amit/AutoCurrency/Helper/Data.php

<?php
 class Amit_AutoCurrency_Helper_Data extends Mage_Core_Helper_Abstract
{
    /**
     * Load GeoIP binary data file
     *
     * @return string
     */
    public function loadGeoIp() 
    {   
        // Load geoip.inc
        include_once(Mage::getBaseDir().'/var/geoip/geoip.inc');

        // Open Geo IP binary data file
        $geoIp = geoip_open(Mage::getBaseDir().'/var/geoip/GeoIP.dat',GEOIP_STANDARD);

        return $geoIp;
    }

    /**
     * Get IP Address
     *
     * @return string
     */
    public function getIpAddress() 
    {   
        //return "124.41.230.51";
        return $_SERVER['REMOTE_ADDR'];
    }
}

app/etc/modules/Amit_AutoCurrency.php

<?xml version="1.0"?>
<config>
  <modules>
      <Amit_AutoCurrency>
          <active>true</active>
          <codePool>local</codePool>
      </Amit_AutoCurrency>
  </modules>
</config>

Download geoip files goes to

https://github.com/maxmind/geoip-api-php

I hope it will be work for you