Giter Site home page Giter Site logo

magefan / module-geoip Goto Github PK

View Code? Open in Web Editor NEW
52.0 4.0 10.0 34.76 MB

Detect Country by IP in Magento 2

License: Open Software License 3.0

PHP 98.55% HTML 1.45%
magento2 magento2-module magento2-extension magento2-plugin magento2-extension-free magento2-free-extension magento2-module-free magento-2 magento-2-module magento-2-extension

module-geoip's Introduction

Magento 2 GeoIP Database Extension by Magefan

Total Downloads Latest Stable Version

This Magento 2 GeoIP module provides you PHP methods for getting customer country by IP, without any additional plugin for PHP.

It is used for Magento 2 Currency Auto Switcher and Magento 2 Auto Language Switcher by Magefan

Requirements

  • Magento Community Edition 2.0.x-2.4.x or Magento Enterprise Edition 2.0.x-2.4.x

Installation Method 1 - Installing via composer

  • Open command line
  • Using command "cd" navigate to your magento2 root directory
  • Run command: composer require magefan/module-geoip
composer require magefan/module-geoip
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy

Installation Method 2 - Installing using archive

  • Install GeoIP2 PHP API (https://github.com/maxmind/GeoIP2-php).
  • Download ZIP Archive
  • Extract files
  • In your Magento 2 root directory create folder app/code/Magefan/GeoIp
  • Copy files and folders from archive to that folder
  • In command line, using "cd", navigate to your Magento 2 root directory
  • Run commands:
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy

How To Use

protected $ipToCountryRepository;

public function __construct(
    \Magefan\GeoIp\Model\IpToCountryRepository $ipToCountryRepository,
    ....//other code
) {
    $this->ipToCountryRepository = $ipToCountryRepository;
    ...//other code
}

public function example() {
    $visitorCountyCode = $this->ipToCountryRepository->getVisitorCountryCode();
    $someCountryCodeByIp = $this->ipToCountryRepository->getCountryCode('104.27.164.57');
    ...//other code
}

Support

If you have any issues, please contact us then if you still need help, open a bug report in GitHub's issue tracker.

Need More Features?

Please contact us to get a quote https://magefan.com/contact

License

The code is licensed under Open Software License ("OSL") v. 3.0.

This product includes GeoLite2 data created by MaxMind, available from https://www.maxmind.com.

Originaly use this databases:

https://www.maxmind.com

http://software77.net/geo-ip/

Magento 2 Point of Sale

Magento 2 Theme

module-geoip's People

Contributors

andriysvyryda avatar denystsymbal avatar ihorvansach avatar kozakserhii avatar magefan avatar termos008 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

module-geoip's Issues

The IP database https://magefan.com/media/geoip/GeoLite2-Country.mmdb is outdated

The module downloads it's IP database here https://github.com/magefan/module-geoip/blob/master/Model/GeoIpDatabase/MaxMind.php#L18

As you can see with curl -sSL -D - https://magefan.com/media/geoip/GeoLite2-Country.mmdb -o /dev/null, that file was last-modified: Fri, 29 Oct 2021 10:27:55 GMT

image

It's about one year old, and you can tell: an IP such as 85.204.70.110 is from France, and https://www.maxmind.com/en/geoip2-precision-demo?ip_address=85.204.70.110 reports this correctly. But the module database is outdated, and so the module reports it as "Great Britain".

This is a by-the-book example of why is a terrible practice to copy and host live data. You should at very least update the database once per month or, even better, allow the user to insert an apikey and get it from maxmind.com directly.

Thanks

Extension crashes Magento admin when Magento_AdminNotification is disabled

Hello magefan team!

Today I stumbled upon this strange error, when entering the admin: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'magento.adminnotification_inbox' doesn't exist, query was: SELECT adminnotification_inbox.* FROM adminnotification_inbox WHERE (title = 'NEW Magento 2 Product Tabs Extension!') AND (url = 'https://magefan.com/magento-2/extensions/product-tabs?utm_source=website&utm_medium=admin-ntf&utm_campaign=product-tabs ')

Our project has the Magento_AdminNotification module disabled, so the adminnotification_inbox was never created in the database. Is it possible that you depend on this table existing in the system without checking? This is quite bad, because is completely disables the admin area by throwing these ugly exceptions:

magefans requires magento adminnotifications

Would be cool if you fixed this :-) Perhaps checking for the existance of the adminnotification_inbox would be enough.

Fatal error addFielToFilter()

Hi,

If php-geoip is not installed, there's a fallback to the database tables but the code breaks in this case.

Here's the error:

Fatal error: Uncaught Error: Call to undefined method Magefan\GeoIp\Model\ResourceModel\IpToCountry\Collection::addFielToFilter() in /var/www/vendor/magefan/module-geoip/Model/IpToCountryRepository.php:56 Stack trace: #0 /var/www/vendor/magefan/module-geoip/Model/IpToCountryRepository.php(74): Magefan\GeoIp\Model\IpToCountryRepository->getCountryCode('127.0.0.1') #1 /var/www/app/code/Clrz/GeoIp/Helper/Popin.php(193): Magefan\GeoIp\Model\IpToCountryRepository->getVisitorCountryCode() #2 /var/www/app/code/Clrz/GeoIp/view/frontend/templates/switch/languages.phtml(35): Clrz\GeoIp\Helper\Popin->getUserCountry() #3 /var/www/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/var/www/projec...') #4 /var/www/vendor/magento/framework/View/Element/Template.php(270): Magento\Framework\View\TemplateEngine\Php->render(Object(Clrz\GeoIP\Block\Store\Switcher\Interceptor), '/var/www/projec...', Array) #5 /var/www/generated/code/Clrz/GeoIP/Block/Store/Switcher/Interce in /var/www/vendor/magefan/module-geoip/Model/IpToCountryRepository.php on line 56

Error in IpToCountryRepository.php : on too many /../../../../../var/magefan/geoip/GeoLite2-Country.mmdb

There is an error here: https://github.com/magefan/module-geoip/blob/master/Model/IpToCountryRepository.php#L128

Since CWD is <magento>/vendor/magefan/module-geoip/Model, if you go up '/../../../../../ you end up in the directory ABOVE Magento, thus var/magefan/geoip/GeoLite2-Country.mmdb is ALWAYS non-existant.

Also, please use the recommended function to get the var directory.

Also, please assign the path to a variable, so you don't have to write the same code twice.

This is my quick and dirty patch:

....
                    $geoDbFile =
                        \Magento\Framework\App\ObjectManager::getInstance()
                            ->get('\Magento\Framework\Filesystem\DirectoryList')
                            ->getPath('var') . DIRECTORY_SEPARATOR . 'magefan/geoip/GeoLite2-Country.mmdb';

                    if ( file_exists($geoDbFile) ) {
                        $datFile = $geoDbFile;

...

                    } else {
....

Even better: use DI instead of ObjectManager::getInstance.

Also: that function is too long. Please put a guard clause at the beginning and split the code in multiple function: were to split is obvious just looking at the code layout.

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.