-2

The script below is being used on my site on a BUY button, to redirect visitor on one online store or the other, depending on their location (country or continent).

I need to retrieve the value of country/continent for other purposes, but as a JS newbie, I can't make it with getgeoip.json.continent_code

// Buy Button 
function getgeoip(json) {
    // json.continent_code = "EU";
    // json.country_code = "AU";
    //window.console && console.log(json);
    if (json.country_code == "US") {
        $("#link-amazon")
            .css("display", "none");
        $("#buybtn")
            .css("display", "block");
    } else if (json.country_code == "GB") {
        $("#link-amazon")
            .css("display", "none");
        $("#buybtn")
            .css("display", "block");
        $("#buybtn")
            .attr("action", "http://shop-us.mystore.io/cart/add ");
        $("#buypdt")
            .attr("value", "8819215683");
    } else if (json.country_code == "AU") {
        $("#link-amazon")
            .css("display", "none");
        $("#price")
            .css("display", "block");
        $("#price2")
            .css("display", "none");
        $("#buybtn")
            .css("display", "block");
        $("#buybtn")
            .attr("action", "https://shop-au.mystore.io/cart/add ");
        $("#buypdt")
            .attr("value", "19889525059");
    } else if (json.continent_code == "EU") {
        $("#price")
            .css("display", "block");
        $("#price2")
            .css("display", "none");
        $("#link-amazon")
            .css("display", "none");
        $("#buybtn")
            .css("display", "block");
        $("#buybtn")
            .attr("action", "http://shop-eu.mystore.io/cart/add ");
        $("#buypdt")
            .attr("value", "8346318915");
    } else {
        $("#link-amazon")
            .css("display", "none");
        $("#buybtn")
            .css("display", "block");
        $("#price")
            .css("display", "none");
        $("#price2")
            .css("display", "block");
        alert('hi');
    }
}
$(document)
    .ready(function () {
        $.ajax({
            url: 'https://telize-v1.p.mashape.com/geoip'
            , type: 'GET'
            , data: {}
            , dataType: 'json'
            , success: function (data) {
                getgeoip(data);
            }
            , error: function (err) {}
            , beforeSend: function (xhr) {
                xhr.setRequestHeader("X-Mashape-Authorization", "KFiiN9M9iImsh57NIcdOcOPVltLDp17HQ6Gjsn5PXFCBAzL8UM"); // Enter here your Mashape key
            }
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form action="http://shop-eu.mystore.io/cart/add" method="post" target="_blank" id="buybtn" style="display: block;">
    <input name="return_to" type="hidden" value="cart">
    <input name="handle" type="hidden" value="mystore">
    <input type="hidden" name="id" value="8739494597" id="buypdt">

    <div class="buy-button-wrapper">
        <button type="submit" id="btn-buy" class="btn-buy" style="margin-top: 0;"><div class="cart"></div>BUY</button>
    </div>
</form>
Adrien Lafond
  • 63
  • 3
  • 12
  • Please explain little more about what you want to achieve, When I run your script its giving me country Code 'AE' which is correct since i am in AE. – Azeez Kallayi Sep 21 '16 at 10:53

1 Answers1

1

i think maybe you can use saas solution. i use freegeoip.

$.getJSON('//freegeoip.net/json/?callback=?', function (data) {
                alert('Your Country is ' +data.country_name);
                console.log(JSON.stringify(data, null, 2));
           });

response:

{
  "ip": "78.187.196.149",
  "country_code": "TR",
  "country_name": "Turkey",
  "region_code": "38",
  "region_name": "Kayseri",
  "city": "Kayseri",
  "zip_code": "",
  "time_zone": "Europe/Istanbul",
  "latitude": 38.7322,
  "longitude": 35.4853,
  "metro_code": 0
}

Detail: How to get client's IP address using javascript only?

Community
  • 1
  • 1
Mahmut Gundogdu
  • 543
  • 4
  • 12