cyanoxide Posted September 16, 2016 Share Posted September 16, 2016 What's the best way to go about grabbing the visitors country as a variable via geolocation? I need to get customers from a certain country to an external website. I'm looking to add a banner to the top of the page asking them to go to the other site but I need to grab the user's country. I've done a bit of searching and I've found people looking to do similar things on both prestashop 1.4 and 1.5 but the methods they have used don't seem to worth for me. I'm assuming this is down to my version of prestashop (1.6) and the method needs to be changed in some way to accommodate for changes made to prestashop. Anyone familiar with how to do this in 1.6? Any help would be greatly appreciated Link to comment Share on other sites More sharing options...
rocky Posted September 17, 2016 Share Posted September 17, 2016 Try using the following in a PHP file: $country = Tools::getCountry(); or the following in a TPL file: {assign var='country' value=Tools::getCountry()} Link to comment Share on other sites More sharing options...
cyanoxide Posted September 21, 2016 Author Share Posted September 21, 2016 Try using the following in a PHP file: $country = Tools::getCountry(); or the following in a TPL file: {assign var='country' value=Tools::getCountry()} That's awesome, thanks. How would you recommend testing it? When I try it it give me "17" that refers to the UK which is when I am so it would appear to be right but when I've tried using VPNs/proxies to see if the number changes to the associated country's number it seems to change to the USA code "21" and not the code associated with the country I've selected. I've asked someone in Romania to test it also and the also get "21" so I don't believe it is working correctly. Link to comment Share on other sites More sharing options...
rocky Posted September 21, 2016 Share Posted September 21, 2016 After reading the function, it seems to get the country from the customer's entered address. If they haven't entered a country yet, it will use the browser's language to choose the country (if "Set default country from browser language" is set to "Yes" on the Localization > Localization tab). Otherwise, it uses the default country set on the Localization > Countries tab. It doesn't appear to use the IP address to decide the country. After closer inspection, it's the FrontController::geolocationManagement function that gets the country by IP address. You can use the following in a TPL file to view the country: {$geolocation_country} Link to comment Share on other sites More sharing options...
cyanoxide Posted September 22, 2016 Author Share Posted September 22, 2016 After reading the function, it seems to get the country from the customer's entered address. If they haven't entered a country yet, it will use the browser's language to choose the country (if "Set default country from browser language" is set to "Yes" on the Localization > Localization tab). Otherwise, it uses the default country set on the Localization > Countries tab. It doesn't appear to use the IP address to decide the country. After closer inspection, it's the FrontController::geolocationManagement function that gets the country by IP address. You can use the following in a TPL file to view the country: {$geolocation_country} Unfortunately I've already tried that one it just returns an empty value when used. I had a suspicious that the reason this might not be working correctly was down to my CDN (cloudflare) I've found an alternative solution through my CDN but I'm not sure how to implement it. In a php file I can use $_SERVER["HTTP_CF_IPCOUNTRY"]; and that grabs the country code from the http header. It seems to work perfectly on a php file and I've tested it through proxies and it give the correct code however I'm not sure how to go about making it a global variable that can be used in the header.tpl file. I've tried adding it to initHeader in FrontController.php but it doesn't seem to be passing through to the tpl. I'll show the code I'm using within the Front Controller, maybe I'm doing it incorrectly. $this->context->smarty->assign(array( 'geo_country' => $_SERVER["HTTP_CF_IPCOUNTRY"], )); and then I'm trying to call it in the tpl with {$geo_country} Link to comment Share on other sites More sharing options...
rocky Posted September 23, 2016 Share Posted September 23, 2016 Try using the following in header.tpl: {$smarty.server.HTTP_CF_IPCOUNTRY} Link to comment Share on other sites More sharing options...
cyanoxide Posted September 23, 2016 Author Share Posted September 23, 2016 I think you've just saved the day with that one, thank you! Link to comment Share on other sites More sharing options...
El Patron Posted September 25, 2016 Share Posted September 25, 2016 (edited) I was curious about this thread given the number of ways a visiitr can be localized, as mentioned above via languge detect set country, geo localization, or default country, 3rd party module etc. The visitor cookie country IMHO has the final say on visitor country. But I do not know how available that is to you or if not what you were really looking for. Edited September 25, 2016 by El Patron (see edit history) Link to comment Share on other sites More sharing options...
cyanoxide Posted September 26, 2016 Author Share Posted September 26, 2016 I was curious about this thread given the number of ways a visiitr can be localized, as mentioned above via languge detect set country, geo localization, or default country, 3rd party module etc. The visitor cookie country IMHO has the final say on visitor country. But I do not know how available that is to you or if not what you were really looking for. I believe the solution provided by Rocky is ideal for what I'm looking for. I'm grabbing this variable so that I can display a banner at the top of the page via the header tpl. I imagine it's not 100% perfect but that's fine. It's there to serve as a notification, if they don't see it then it isn't the end of the world but it would save them a few minutes. Anything more than that would be over complicating it in my opinion. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now