You are here

public function CurrencyHelper::getUserCountry in Commerce Currency Resolver 8

Get user country location from contrib modules.

Return value

mixed Return 2 letter country code.

Overrides CurrencyHelperInterface::getUserCountry

File

src/CurrencyHelper.php, line 169

Class

CurrencyHelper
Helper for various parts of the module.

Namespace

Drupal\commerce_currency_resolver

Code

public function getUserCountry() {
  $service = $this->configFactory
    ->get('commerce_currency_resolver.settings')
    ->get('currency_geo');
  switch ($service) {
    case 'smart_ip':
      $country = \Drupal::service('smart_ip.smart_ip_location')
        ->get('countryCode');
      break;
    case 'geoip':
      $ip_address = $this->requestStack
        ->getCurrentRequest()
        ->getClientIp();
      $country = \Drupal::service('geoip.geolocation')
        ->geolocate($ip_address);
      break;
  }

  // If geolocation fails for any specific reason (most likely on local
  // environment, use default country from Drupal.
  if (empty($country)) {
    $country = $this->configFactory
      ->get('system.date')
      ->get('country.default');
  }
  return $country;
}