4

I'm developing a site using opencart in which I plan to support multiple currencies. The currency should change based on the user’s physical location; that is, if a person opens it from the USA the currency visibility is set dollars, and if person in India opens it, it should be set to INR. Is that possible?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
venkat
  • 111
  • 1
  • 7

2 Answers2

0

Try this:

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    console.log("Geolocation is not supported by this browser.");
  }
}

function showPosition(position) {
  GetAddress(position.coords.latitude, position.coords.longitude)
}

function GetAddress(lat, lng) {
  var latlng = new google.maps.LatLng(lat, lng);
  var geocoder = geocoder = new google.maps.Geocoder();
  geocoder.geocode({
    'latLng': latlng
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      if (results[0]) {
        var address = results[1].address_components;
        for (var i = 0, iLen = address.length; i < iLen; i++) {
          if (address[i].types[0] === 'country') {
            var countryName = address[i].long_name;
            alert(countryName);
          }
        }
      }
    }
  });
}
<button type="button" onclick="getLocation()">Get Location</button>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

Plunker here

Rayon
  • 36,219
  • 4
  • 49
  • 76
  • 1
    thanks bro, i got it and how the currency conversion will happen based on that geo location – venkat Sep 24 '15 at 07:07
  • You need to have a Databse where you can map **currencies** to **country name**, other option is to use `third-party-apis` to do this for you. You can accept this as an answer if it has helped you.. – Rayon Sep 24 '15 at 07:14
  • I would suggest you to this @ php end as that seems more robust solution. Better option is to use `ip-address` of the user as user my deny to provide his location. – Rayon Sep 24 '15 at 07:25
  • actually i am using open cart, how can i do that – venkat Sep 24 '15 at 09:43
0

You can do this by using GeoLocation,Here is the code

 $('.currency').text(geoplugin_currencyCode());  
       $('.symbol').html(geoplugin_currencySymbol());  
<script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript">
    </script>
<h6><span class="symbol"></span>90 <span class="currency"></span></h6>
Karan Goyal
  • 447
  • 5
  • 10