1

Hi all and thanks for reading, I'm working on magento and we are using geoip extension to locate customer and then deside the correct currency depending on his location. this was working good if I set one country for each allowed currency and USD for rest of world countries (like AED for united arab emarites and so on ..). but I was asked to view drop down in header containing the country currency + USD (which is the Default Display Currency). what I did is adding all countries to USD currency (to be allowed to use USD currency) and added my drop down, every thing was ok and the currency is changing from the drop down. tell now every thing is good, but my problem is the Default Display Currency is what used on first page load not the country currency (which is displayed in the dorp down next to USD currency). How can I force the system to use country currency instead of using USD on first page load?? any help, any idea is appreciated. Thanks in advance.

rramiii
  • 1,156
  • 6
  • 15
  • 29

1 Answers1

4

if u want to select default currency according to visitor's country.follow this steps.(No Extension of magento)

  1. download https://www.maxmind.com/download/geoip/api/php-20120410/geoip.inc and http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
  2. extract GeoIP.dat.gz and make one folder in root name it as geoip. save geoip.inc and GeoIP.dat in that folder.
  3. copy /app/code/core/Mage/Directory/Block/Currency.php into local/Mage/Directory/Block/Currency.php
  4. Use Below code to get current visitor's country.

    define("GEOIP_DAT_FILE", $_SERVER['DOCUMENT_ROOT'] . "/geoip/GeoIP.dat");
    define("GEOIP_INC_FILE", $_SERVER['DOCUMENT_ROOT'] . "/geoip/geoip.inc");
    include(GEOIP_INC_FILE);
    
    $_geoip = geoip_open(GEOIP_DAT_FILE ,GEOIP_STANDARD);
    $_country_code = geoip_country_code_by_addr($_geoip, $_SERVER['REMOTE_ADDR']);
    geoip_close($_geoip);
    
  5. in method getCurrentCurrencyCode(). set something like this, (modify as your requirement.)

    public function getCurrentCurrencyCode()
    {
        if (is_null($this->_getData('current_currency_code'))) {
            // do not use Mage::app()->getStore()->getCurrentCurrencyCode() because of probability
            // to get an invalid (without base rate) currency from code saved in session
            switch ($_country_code) {
            case 'IN':
                $currency_code = 'INR';
                break;              
            case 'FR':
                $currency_code = 'EUR';
                break;
            default:
                $currency_code = 'USD';
                break;
            }
    
            $this->setData($currency_code, Mage::app()->getStore()->getCurrentCurrency()->getCode());
        }
    
        return $this->_getData('current_currency_code');
    }
    

Hope, This'll Help You.

Niraj Jani
  • 427
  • 5
  • 14
  • This code will set default currency and from dropdown we can select differnt currency if we want, that won't be affected. – Niraj Jani Sep 08 '14 at 06:41
  • Thank you, yes this code will but on first page load will use the default currency used for the store, I think I found the solution in getCurrentCurrencyCode() function and now I'm trying to handle the case of multiple currencies allowed for one country, here I have to make the page use country's currency on first page load (not any of the other countries currencies that are allowed for this country). when I finish my solution will post it :) – rramiii Sep 08 '14 at 08:19