diff --git a/modules/smart_ip/commerce_currency_resolver_smart_ip.routing.yml b/modules/smart_ip/commerce_currency_resolver_smart_ip.routing.yml index fef9fd9..a2ec3d4 100644 --- a/modules/smart_ip/commerce_currency_resolver_smart_ip.routing.yml +++ b/modules/smart_ip/commerce_currency_resolver_smart_ip.routing.yml @@ -2,14 +2,14 @@ commerce_currency_resolver_smart_ip.countries.autocomplete: path: '/admin/commerce/config/commerce_currency_resolver/autocomplete' defaults: - _controller: '\Drupal\commerce_currency_resolver_smart_ip\Controller\CountryCurrencyResolverAutocomplete::countriesAutocomplete' + _controller: '\Drupal\commerce_currency_resolver\Controller\CountryCurrencyResolverAutocomplete::countriesAutocomplete' requirements: # use OR conjunction _permission: 'administer commerce currency resolver settings' commerce_currency_resolver_smart_ip.currency_mapping: path: '/admin/commerce/config/commerce_currency_resolver/geoip' defaults: - _form: 'Drupal\commerce_currency_resolver_smart_ip\Form\CurrencyResolveGeoipMapping' + _form: 'Drupal\commerce_currency_resolver_smart_ip\Form\CurrencyResolveSmartIpMapping' _title: 'Geo mapping' requirements: _permission: 'administer commerce currency resolver settings' diff --git a/modules/smart_ip/src/Resolver/CurrencyResolverSmartIp.php b/modules/smart_ip/src/Resolver/CurrencyResolverSmartIp.php index 4d58ba9..33ad72d 100644 --- a/modules/smart_ip/src/Resolver/CurrencyResolverSmartIp.php +++ b/modules/smart_ip/src/Resolver/CurrencyResolverSmartIp.php @@ -5,6 +5,7 @@ namespace Drupal\commerce_currency_resolver_smart_ip\Resolver; use Drupal\commerce_currency_resolver\CurrencyResolverManagerInterface; use Drupal\commerce_price\Entity\CurrencyInterface; use Drupal\commerce_price\Resolver\CurrencyResolverInterface; +use Drupal\smart_ip\SmartIp; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\smart_ip\SmartIpLocation; use Symfony\Component\HttpFoundation\RequestStack; @@ -25,15 +26,35 @@ class CurrencyResolverSmartIp implements CurrencyResolverInterface { * {@inheritdoc} */ public function resolve(): ?CurrencyInterface { - $user_ip = $this->requestStack->getCurrentRequest()?->getClientIp(); - if ($user_ip) { - $country = $this->geoLocation->get('countryCode'); - $matrix = $this->configFactory->get('commerce_currency_resolver_smart_ip.currency_mapping')->get('matrix'); + $matrix = $this->configFactory->get('commerce_currency_resolver_smart_ip.currency_mapping')->get('matrix'); + if (empty($matrix) || !is_array($matrix)) { + return NULL; + } + + $user_ip = $this->requestStack->getCurrentRequest()?->getClientIp(); + if (!$user_ip) { + return NULL; + } + + $country = strtoupper(trim((string) $this->geoLocation->get('countryCode'))); + if ($country === '') { + // If Smart IP location has not been populated in the current request + // yet, fallback to a direct lookup by client IP. + try { + $location = SmartIp::query($user_ip); + $country = strtoupper(trim((string) ($location['countryCode'] ?? ''))); + } + catch (\Throwable) { + return NULL; + } + } - if (isset($matrix[$country])) { - return $this->currencyResolverManager->getCurrencyByCode($matrix[$country]); - } + if (isset($matrix[$country])) { + return $this->currencyResolverManager->getCurrencyByCode($matrix[$country]); } return NULL; }