4

How to set currency depending on country's IP, means if customer selects India country on checkout step then currency should be set Indian Rupee automatically. My Default Currency is 'USD'.

4
  • are you using different stores or store views ? Jul 22, 2016 at 5:35
  • yes i am use diffrent store views. Jul 22, 2016 at 5:48
  • do you want show currency chnage only after selecting country as india , dont you want to show on product pages and other pages ? means if customer visit from us, it should show $ Jul 22, 2016 at 5:50
  • ya i want exactly that you asked... can you help me.. Jul 22, 2016 at 5:54

3 Answers 3

3

Credits : Raphael at Digital Pianism

you can use Geo IP MAGENTO STORE SWITCHER BASED ON CUSTOMER LOCATION.

Follow this tutorial , you can find complete code here

I am posting code, just if link become 404 page

/* app/code/local/Atwix/Ipstoreswitcher/Helper/Data.php */
class Atwix_Ipstoreswitcher_Helper_Data extends Mage_Core_Helper_Abstract
{
    const DEFAULT_STORE = 'English';

    /**
     * countries to store relation
     * default is English
     * @var array
     */
    protected $_countryToStore = array(
        'FR' => 'French',
        'BE' => 'French',
        'CH' => 'French',
        'DE' => 'German',
        'AT' => 'German',
        'UK' => 'English',
        'US' => 'English',
        'UA' => 'English',
        'CN' => 'English',
        'JP' => 'English'
    );

    /**
     * get store view name by country
     * @param $country
     * @return bool
     */
    public function getStoreByCountry($country)
    {
        if (isset($this->_countryToStore[$country])) {
            return $this->_countryToStore[$country];
        }
        return self::DEFAULT_STORE;
    }
}

<!--app/code/local/Atwix/Ipstoreswitcher/etc/config.xml-->
<?xml version="1.0"?>       
<config>
 ...
    <global>
    ...
    </global>
    <frontend>
        <events>
            <controller_action_postdispatch>
                <observers>
                    <atwix_ipstoreswitcher>
                        <class>atwix_ipstoreswitcher/observer</class>
                        <method>controllerActionPostdispatch</method>
                    </atwix_ipstoreswitcher>
                </observers>
            </controller_action_postdispatch>
        </events>
    </frontend>
</config>

/* app/code/local/Atwix/Ipstoreswitcher/Model/Observer.php */

class Atwix_Ipstoreswitcher_Model_Observer
{
    /**
     * redirects customer to store view based on GeoIP
     * @param $event
     */
    public function controllerActionPostdispatch($event)
    {
        $cookie = Mage::getSingleton('core/cookie');
        if ($cookie->get('geoip_processed') != 1) {
            $geoIPCountry = Mage::getSingleton('geoip/country');
            $countryCode = $geoIPCountry->getCountry();
            if ($countryCode) {
                $storeName = Mage::helper('atwix_ipstoreswitcher')->getStoreByCountry($countryCode);
                if ($storeName) {
                    $store = Mage::getModel('core/store')->load($storeName, 'name');
                    if ($store->getName() != Mage::app()->getStore()->getName()) {
                        $event->getControllerAction()->getResponse()->setRedirect($store->getCurrentUrl(false));
                    }
                }
            }
            $cookie->set('geoip_processed', '1', time() + 86400, '/');
        }
    }
}
1
  • Determining default location and possibly even currency based on GeoIP makes sense. But why would you determine language based on IP instead of using the Accept-Language http header? Consider the Swiss, which has three different languages (German, French and Italian) or consider somebody on vacation, using a proxy or who simply prefers English over their local language. Jul 22, 2016 at 8:46
1

You must care about checkout currency. GeoIP and auto switch store can help you change display currency based on country, but when customer checkout Magento will use base currency as default

1

I'm personally using this open source extension to detect a user's country and set the currency automatically.

https://github.com/chapagain/auto-currency-switcher

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.