0

I have changed the default country of store by below setting.

Admin -> System Configuration -> General -> Countries options -> Default country

But, now I want to change the country selected based on Customer GeoIP.

How can I do this?

1 Answer 1

1

Firstly, you need to get the customer IP address information by doing code as below:

Magento\Framework\HTTP\PhpEnvironment\RemoteAddress

Add this above code to construct function in your file and after that get the Ip address for the customer by using "$this->remoteAddress->getRemoteAddress();", this way you will get an IP address. Now second based on IP address you need to grep the information of address where the customer belongs to. You can use this code to fetch customer address information.

$ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=x.x.x.x"));

You will get the country code and other information as well.

1
  • 1
    You don't really expect that on each page view the geo json data should be fetched and parsed by the server? The site you mention uses the Maxmind database, a much better solution would be to implement the maxmind geo lookup locally in a custom module and perform cached ip lookups on a local geo ip database.
    – paj
    Dec 29, 2020 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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