Detect IP address and redirect customers straight to their language page

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 年 前
Detect IP address so that non-U.S./Canada/Mexico/Australia customers go straight to the OTHER section of the global contact area.
8 年 前
I have the same problem.
8 年 前
This function is not available in nopCommerce. You have to change core code for that.
7 年 前
We added this feature for one of our customers and extended the Applcation_BeginRequest in Global.asax

var _geoLookUpService = EngineContext.Current.Resolve<IGeoLookupService>();
string countryCode = _geoLookUpService.LookupCountryIsoCode(webHelper.GetCurrentIpAddress());
if(!String.IsNullOrEmpty(countryCode))
{
     var _storeService = EngineContext.Current.Resolve<IStoreService>();
     var curHost = webHelper.GetStoreLocation();
     var countryStoretUrl = string.Format("{0}{1}", curHost, countryCode).ToLowerInvariant();
     var countryStore = _storeService.GetAllStores().SingleOrDefault(s => s.Hosts.Contains(countryStoretUrl.TrimStart("http://".ToCharArray())));

     if (countryStore != null)
     {
         //_logger.InsertLog(LogLevel.Information, string.Format("Request {0} is from {1} and match store {2}", curPage, countryCode.ToLowerInvariant(), countryStore.Name));

         if (!countryStore.Url.Equals(curHost))
         {
             var redirectUrl = string.Format("{0}{1}", countryStore.Url, this.Request.RawUrl);
             this.Response.Redirect(redirectUrl);
         }
     }
}
6 年 前
Hi,

This plugin can help you
http://thenopplugins.com/store-ip-detector
6 年 前
objecta wrote:
We added this feature for one of our customers and extended the Applcation_BeginRequest in Global.asax

var _geoLookUpService = EngineContext.Current.Resolve<IGeoLookupService>();
string countryCode = _geoLookUpService.LookupCountryIsoCode(webHelper.GetCurrentIpAddress());
if(!String.IsNullOrEmpty(countryCode))
{
     var _storeService = EngineContext.Current.Resolve<IStoreService>();
     var curHost = webHelper.GetStoreLocation();
     var countryStoretUrl = string.Format("{0}{1}", curHost, countryCode).ToLowerInvariant();
     var countryStore = _storeService.GetAllStores().SingleOrDefault(s => s.Hosts.Contains(countryStoretUrl.TrimStart("http://".ToCharArray())));

     if (countryStore != null)
     {
         //_logger.InsertLog(LogLevel.Information, string.Format("Request {0} is from {1} and match store {2}", curPage, countryCode.ToLowerInvariant(), countryStore.Name));

         if (!countryStore.Url.Equals(curHost))
         {
             var redirectUrl = string.Format("{0}{1}", countryStore.Url, this.Request.RawUrl);
             this.Response.Redirect(redirectUrl);
         }
     }
}


Hi,

I have included this code in and user is being redirected to the correct url. However, there is an issue when user gets redirected.
For example, my default site for all users is aaa.com and specifically for user from country b, it will be redirected to aaa.com.b. Currently, it does redirect aaa.com.b but shows an error as below. If i type into browser manually aaa.com.b, it works perfectly.

Redirected Error :
Webpage not available
The webpage could not be loaded because:
Proxy Error

Any idea on this?
3 年 前
We can redirect country vise like below in Nop version 4.0 and above. You can add OnActionExecuting in Base controller like below.

public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var _workContext = EngineContext.Current.Resolve<IWorkContext>();
            var _storeContext = EngineContext.Current.Resolve<IStoreContext>();            
            var _webHelper = EngineContext.Current.Resolve<IWebHelper>();
            var _logger = EngineContext.Current.Resolve<ILogger>();
            var geo = EngineContext.Current.Resolve<IGeoLookupService>();
      
            string country = geo.LookupCountryIsoCode(_webHelper.GetCurrentIpAddress());
            _logger.InsertLog(Core.Domain.Logging.LogLevel.Information, "Country Origin", country, _workContext.CurrentCustomer);

            if (!string.IsNullOrEmpty(country))
            {
                if (country.Equals("US") && _storeContext.CurrentStore.Hosts.ToLower() != "abc.com")
                {
                    _logger.InsertLog(Core.Domain.Logging.LogLevel.Information, "Redirection To US Country:" + country, _webHelper.GetCurrentIpAddress(), _workContext.CurrentCustomer);
                    filterContext.Result = new RedirectResult("http://abc.com");
                    return;
                }
                if (country.Equals("CA") && _storeContext.CurrentStore.Hosts.ToLower() != "def.com")
                {
                    _logger.InsertLog(Core.Domain.Logging.LogLevel.Information, "Redirection TO CA Country:" + country, _webHelper.GetCurrentIpAddress(), _workContext.CurrentCustomer);
                    filterContext.Result = new RedirectResult("http://def.com");
                    return;
                }
                if (country != "CA" && country != "US")
                {
                    if (_storeContext.CurrentStore.Hosts.ToLower() != "site.com")
                    {
                        _logger.InsertLog(Core.Domain.Logging.LogLevel.Information, "Redirection to other Country:" + country, _webHelper.GetCurrentIpAddress(), _workContext.CurrentCustomer);
                        filterContext.Result = new RedirectResult("http://site.com");
                        return;
                    }
                }
            }
        }

User can change as per requirement.
3 年 前
var _webHelper = Nop.Core.Infrastructure.EngineContext.Current.Resolve<IWebHelper>();
                var res = (_webHelper.GetCurrentIpAddress());
                var url = _webHelper.GetStoreHost(false);
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("https://geolocation-db.com/json/" + res);

                // Set some reasonable limits on resources used by this request
                request.MaximumAutomaticRedirections = 4;
                request.MaximumResponseHeadersLength = 4;
                // Set credentials to use for this request.
                request.Credentials = System.Net.CredentialCache.DefaultCredentials;
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();

                // Get the stream associated with the response.
                System.IO.Stream receiveStream = response.GetResponseStream();

                // Pipes the stream to a higher level stream reader with the required encoding format.
                System.IO.StreamReader readStream = new System.IO.StreamReader(receiveStream, System.Text.Encoding.UTF8);

                var resp = readStream.ReadToEnd();
                response.Close();
                readStream.Close();
                var resdets = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(resp);
                //based on response do what ever you need
3 年 前
@Globalbasket
And why would I want to use your 15+ lines of code rather than just use nopCommerce's IGeoLookupService?
3 年 前
Did you try GeoLookupService ?

GetInformation(string ipAddress) --> This should give you the Geo Information from the IP Address.


Globalbasket wrote:
var _webHelper = Nop.Core.Infrastructure.EngineContext.Current.Resolve<IWebHelper>();
                var res = (_webHelper.GetCurrentIpAddress());
                var url = _webHelper.GetStoreHost(false);
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("https://geolocation-db.com/json/" + res);

                // Set some reasonable limits on resources used by this request
                request.MaximumAutomaticRedirections = 4;
                request.MaximumResponseHeadersLength = 4;
                // Set credentials to use for this request.
                request.Credentials = System.Net.CredentialCache.DefaultCredentials;
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();

                // Get the stream associated with the response.
                System.IO.Stream receiveStream = response.GetResponseStream();

                // Pipes the stream to a higher level stream reader with the required encoding format.
                System.IO.StreamReader readStream = new System.IO.StreamReader(receiveStream, System.Text.Encoding.UTF8);

                var resp = readStream.ReadToEnd();
                response.Close();
                readStream.Close();
                var resdets = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(resp);
                //based on response do what ever you need
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.