I am trying to look up the visitor's location and show this on the product page. It's going to be used to send them a personalised message based on location but currently, I am struggling to get it to show the current state.

If I put the code in a normal PHP file and load this it works fine but when I try this on a custom block in a .phthm file I get an error loading the product page saying undefined variable.

I can pull product details but not sure how I correctly define and show this on a product page.

Exception #0 (Exception): Notice: Undefined variable: ip in /home/staging/public_html/app/code/drumbeat/tabmodule/view/frontend/templates/shipping2.phtml on line 76


function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
    $output = NULL;
    global $ip;
    global $state;

    if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
        $ip = $_SERVER["REMOTE_ADDR"];
        if ($deep_detect) {
            if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_CLIENT_IP'];
    $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
    $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
    $continents = array(
        "AF" => "Africa",
        "AN" => "Antarctica",
        "AS" => "Asia",
        "EU" => "Europe",
        "OC" => "Australia (Oceania)",
        "NA" => "North America",
        "SA" => "South America"
    if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
        $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
        if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
            switch ($purpose) {
                case "location":
                    $output = array(
                        "city"           => @$ipdat->geoplugin_city,
                        "state"          => @$ipdat->geoplugin_regionName,
                        "country"        => @$ipdat->geoplugin_countryName,
                        "country_code"   => @$ipdat->geoplugin_countryCode,
                        "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
                        "continent_code" => @$ipdat->geoplugin_continentCode
                case "address":
                    $address = array($ipdat->geoplugin_countryName);
                    if (@strlen($ipdat->geoplugin_regionName) >= 1)
                        $address[] = $ipdat->geoplugin_regionName;
                    if (@strlen($ipdat->geoplugin_city) >= 1)
                        $address[] = $ipdat->geoplugin_city;
                    $output = implode(", ", array_reverse($address));
                case "city":
                    $output = @$ipdat->geoplugin_city;
                case "state":
                    $output = @$ipdat->geoplugin_regionName;
                case "region":
                    $output = @$ipdat->geoplugin_regionName;
                case "country":
                    $output = @$ipdat->geoplugin_countryName;
                case "countrycode":
                    $output = @$ipdat->geoplugin_countryCode;
    // return $output;

    <p><?php echo $ip('State'); ?></p>

Instead of this method I used jQuery Instead as below,

In case helps anyone:

jQuery(document).ready(function($) {
$.get("https://ipinfo.io/json", function (response) {
    $("#ip").append("Your IP is: " + response.ip);
    var regionChk = response.region;
    switch (regionChk)
           case "England":
            $("#region").append("100% FREE 1-Day Delivery " + response.region);

                case "Wyoming":
            $("#region").append("100% FREE 2-Day Delivery ");

               $("#region").append('100% FREE 3-Day US Shipping' + response.region);
    }, "jsonp");
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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