Auto set default store currency to the visitor’s location currency in magento

Auto set default store currency to the visitor’s location currency in magento

You have setup to use multi currencies for your store and now you want your visitors see their’s location currency so they don’t have to switch on themself  ? In this tutorial I will guide you how easy you can set default store currency based on  visitor’s geographic location


1) How magento set default store currency ?

Firstly we would need to detect how magento set the default store currency when someone land on the site, let take a look at magento currency switcher, we look into  the file app/design/frontend/base/default/template/directory/currency.phtml

here you will see

<option value="<?php echo $this->getSwitchCurrencyUrl($_code) ?>"<?php if($_code==$this->getCurrentCurrencyCode()): ?>
 selected="selected"<?php endif; ?>>

you can see that the call  $this->getCurrentCurrencyCode() return the default store code,we take a look at this function in the block file

of  app/code/core/Mage/Directory/Block/Currency.php

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
$this->setData('current_currency_code', Mage::app()->getStore()->getCurrentCurrency()->getCode());

return $this->_getData('current_currency_code');

From this function we can see that the Store class should contain a function name getCurrentCurrency, check further this function in  app/code/core/Mage/Core/Model/Store.php we see that the default currency is set  to a value from session or to the store’s default currency if no session value is available.

This function rely on the getCurrentCurrencyCode() method which in turn retrieves it’s default currency code from the getDefaultCurrencyCode() method

public function getDefaultCurrencyCode()
$result = $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_DEFAULT);
return $result;

This function simply get the system config value you set as default store currency in your back-end

2) Override Magento core function

So to this step it’s clear that if we want automatically set the default store currency to visitor’s location currency we would need to override the class file app/code/core/Mage/Core/Model/Store.php and the getDefaultCurrencyCode method
You can read more at the “How to override magento core blocks, controllers, models, helpers” blog for more about overriding core magento models, here we just need to copy the file app\code\core\Mage\Core\Model\Store.php and paste it to app\code\local\Mage\Core\Model (you need to create folders if they are not there)
Open the copied file you replace it’s method getDefaultCurrencyCode with this

   public function getDefaultCurrencyCode()
        $result = $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_DEFAULT);

        $url = ''.'$_SERVER['REMOTE_ADDR']';
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);

        $myarray = unserialize($data);
        $currencyCode = $myarray['geoplugin_currencyCode'];
        $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();      

        if(!in_array($currencyCode, $allowedCurrencies)) {
            return $result;
            return $currencyCode;

That’s it, let try and if you have any problems please don’t histate leave a comment below

About Dat huang

Dat is a founder and Developer at Mageaddon.He is a magento lover, he enjoys listening music, playing table tennis, swimming whenever he is Free


  1. Many thanks for sharing this information so freely. You need to think about writing a book.

  2. Yesterday, while I waѕ at worκ, my sister
    ѕtοle my іρhone and tested to ssee if it cаn ѕurvive a 25 foot drop, just so she can bе a уoutubе sensatіon.

    Мy iPad іs now destrοyed and she haas 83 views.
    I knοw this is comрletely off topiс but I had too share
    it wirh someone!

  3. MagestyApps has paid extension with same functionality. Yes, it’s paid but with extension you get bonuses like guarantees and good support

Leave a Reply

Your email address will not be published. Required fields are marked *