<?php
namespace PixieMedia\General\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\ScopeInterface;
use Amasty\Base\Model\GetCustomerIp;
use Amasty\Geoip\Helper\Data as GeoipHelper;
use Amasty\Geoip\Model\Geolocation;
class Geo extends AbstractHelper
{
/**
* @var GeoipHelper
*/
private $geoipHelper;
/**
* @var GetCustomerIp
*/
private $customerIp;
/**
* @var Geolocation
*/
private $geolocation;
/**
* @param Context $context
* @param \Amasty\Geoip\Helper\Data $geoipHelper
* @param \Amasty\Geoip\Model\Geolocation $geolocation
* @param \Amasty\Base\Model\GetCustomerIp $customerIp
*/
public function __construct(
Context $context,
GeoipHelper $geoipHelper,
Geolocation $geolocation,
GetCustomerIp $customerIp
) {
parent::__construct($context);
$this->geoipHelper = $geoipHelper;
$this->geolocation = $geolocation;
$this->customerIp = $customerIp;
}
/**
* Check if the country is the UK.
* @return bool
*/
public function isUK()
{
$country = $this->getCountry();
return empty($country) || $country === 'GB';
}
/**
* Get the country based on the current IP.
* @return string
*/
public function getCountry()
{
$currentIp = $this->getCurrentIp();
$location = $this->geolocation->locate($currentIp);
$country = $location->getCountry();
return $country;
}
/**
* Get the current IP address.
* @return string
*/
public function getCurrentIp()
{
if ($this->geoipHelper->isForcedIpEnabled() && $this->geoipHelper->getForcedIp()) {
return $this->geoipHelper->getForcedIp();
}
return $this->customerIp->getCurrentIp();
}
}
Created
July 18, 2024 11:28
-
-
Save DominicWatts/c506845d6732e457c0e7f9c44d93ad25 to your computer and use it in GitHub Desktop.
Magento 2 : Geo IP get country using Amasty #magento2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment