Country Redirect between store tenants

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 years ago
Looking form some advice;

Need to put a rule in to do a store redirect based on visiting IP's country. Not needing 100% here, but effectively a country redirect between US and non US.
Someone from Europe hits the US .com site, they get redirected to the Europe store (.co.uk) sort of thing (another tenant in the store, but different pricing). Note: not the same as price by currency as I want to hide the prices for the majority.

I looked at creating Session_Start in global.asax, but can't redirect from there (but seems like a great place if I could).
So for prototyping, I just used the home controller (default landing page), and did a routine like the following;
But I am thinking a better appraoch might be to do something with the creation of the guest account itself and stuff in a country as an attribute there...this would eliminate me from doing a geo lookup on future visits.
Suggestions are welcome.

New Home Controller Index for doing this on the main landing page.

public ActionResult Index()
        {
            
            var _workContext = EngineContext.Current.Resolve<IWorkContext>();
            var _webHelper = EngineContext.Current.Resolve<IWebHelper>();
            var _logger = EngineContext.Current.Resolve<ILogger>();

            Nop.Services.Directory.GeoLookupService geo = new Nop.Services.Directory.GeoLookupService(_logger, _webHelper);

            string country = geo.LookupCountryIsoCode(_webHelper.GetCurrentIpAddress());
            _logger.InsertLog(Core.Domain.Logging.LogLevel.Information, "Country Origin", country, _workContext.CurrentCustomer);

            string url = _webHelper.GetThisPageUrl(true);

            if (country.Equals("US"))
            {

                if (!url.ToLowerInvariant().Contains("www.blah.com"))
                {
                    _logger.InsertLog(Core.Domain.Logging.LogLevel.Information, "Redirection To US", _webHelper.GetCurrentIpAddress(), _workContext.CurrentCustomer);
                    return Redirect("http://www.blah.com"); //my us store (tenant within the same database)
                }
                else
                    return View();
            }
            else
            {
                // all other sites
                if (!url.ToLowerInvariant().Contains("uk.blah.com"))
                {
                    _logger.InsertLog(Core.Domain.Logging.LogLevel.Information, "Redirection Outside US", _webHelper.GetCurrentIpAddress(), _workContext.CurrentCustomer);
                    return Redirect("http://uk.blah.com");  //my uk store different subdomain (tenant within the same database)
                }
                else
                    return View();
            }
}
10 years ago
I finished implementing this by placing the geoCode lookup in the WebWorkContext file and is triggered on the CurrentCustomer function.  I just used the AdminComment field to stuff the countrycode in rather than update the customer entity as I wasn't using it for anything.
Now, the lookup is very fast in the main controllers and redirects great.
I guess the only area I could improve on is if there is a better location to place the soft check in the nop.web controllers. Any thoughts from anyone?  

Side note..Andrei, I will give you this code if you want to include as part of the core offering (forced auto forward to store based on country origin of user).
9 years ago
Hey Chuck,

would you be able to provide your latest code for this solution? I would really like to implement something similar, so that on any URL that gets triggered the location check is done and a user gets forwarded to the appropriate store.

Thanks,
Stephan
9 years ago
Hello Chuck,
I am trying to do the same thing. I would appreciate if you can please share the code.

Thank you,
Ashima
9 years ago
Hello All,
I am trying to do the same thing.Looking forward to find a solution how we can redirect visitors from specific countries to another domain? I read an articles about the situation,so i think there is a method  Geolocation,through that we can do this.
I would appreciate if you can please share the code.




Thank you,
Rahul
9 years ago
I put my "how to" to a very similar question the other day. See link below.
Note: In my specific case, I only wanted to redirect in certain actions, but the approach can still be used as a foundation if your requirements are different.

https://www.nopcommerce.com/boards/t/34307/geolocation-redirect-per-country.aspx
9 years ago
Thanks Chuck.....


But,I am new here.Not able to understand,Please help me in a simpler way.




Thanks....
3 years ago
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.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.