Thursday, September 8, 2011

How To Set Default Store Currency Based On Visitor Country In Magento?

Installing php-geoip:

If you have full control over your server running Magento, then this task is really easy. Log in with a sudo account or root and just use your distro’s package manager to install php-geoip. On Ubuntu/Debian, all you need to do is:
sudo apt-get install php5-geoip
For other distros, you may need to google around a little.

Now we will create module.
Create file "/app/code/local/MageTalks/Custom/etc/config.xml"

    
        
            0.1
        
    
    
        
            
                
                    MageTalks_Custom_Model_Store
                
            
        
    


Then in "/app/code/local/MageTalks/Custom/Model/Store.php"
class MageTalks_Custom_Model_Store extends Mage_Core_Model_Store
{
    public function getDefaultCurrencyCode()
    {
        // by default we look for the configured currency code
        $result = $this->getConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_DEFAULT);
        // however, in our case we want to determine the default currency depending on the country/continent of the visitor
        $geoCountryCode = null;
        try {
                // we'll try to get the visitor country
            $geoCountryCode = geoip_country_code_by_name( $_SERVER['REMOTE_ADDR'] );
        } catch (Exception $e) {
            // prevent NOTICE error - for example we are running the code on a localhost
        }
        // first tier check is the specific countries to set the currency for
        // NOTE: you can roll your own logic here depending on your enabled/supported countries/currencies
        // this example assumes that AUD, GBP, JPY, USD, EUR and NZD are the supported currencies
        switch ($geoCountryCode) {
            case "AU":
                $result = "AUD";
                break;
            case "GB":
                $result = "GBP";
                break;
            case "JP":
                $result = "JPY";
                break;
            default:
                $geoContinentCode = null;
                // now grab the continent code and set further by general regions
                try {
                    $geoContinentCode = geoip_continent_code_by_name( $_SERVER['REMOTE_ADDR'] );
                                // You can debug here to check the continent codes...
                    Mage::log("GEOIP::CONTINENT::".$geoCountryCode);
                    } catch (Exception $e) {
                    Mage::log("EXCEPTION CAUGHT".$e->getMessage());
                    // prevent NOTICE error
                }
                        // Now decide what currency to set depending on broad regions
                switch ($geoContinentCode) {
                    case "EU": // we'll set EUR for European countries
                        $result = "EUR";
                        break;
                    case "NA": // North America
                    case "SA": // South America
                    case "AS": // Asia
                    case "AF": // Africa - all of them will see USD
                        $result = "USD";
                        break;
                    default:    // everything else uses the default store currency as set in config
                }
        }
        return $result;
    }
}
Then all we need is the module xml file in "/app/etc/modules/MageTalks_Custom.xml"

    
            
            true
            local
                
                     
                
            
    

No comments:

Post a Comment