Scheme::SCHEME_PL, 'RO' => Scheme::SCHEME_RO, 'AT' => Scheme::SCHEME_AT, 'DE' => Scheme::SCHEME_DE, 'SK' => Scheme::SCHEME_SK, 'HU' => Scheme::SCHEME_HU, 'DK' => Scheme::SCHEME_DK, 'SE' => Scheme::SCHEME_SE, 'FI' => Scheme::SCHEME_FI, 'EE' => Scheme::SCHEME_EE, 'LV' => Scheme::SCHEME_LV, 'NL' => Scheme::SCHEME_NL, 'IE' => Scheme::SCHEME_IE, 'HR' => Scheme::SCHEME_HR, 'MT' => Scheme::SCHEME_MT, 'PT' => Scheme::SCHEME_PT, ]; /** * @param array $settings */ public function __construct(private array $settings) { } /** * Determine active scheme ID for the given country code or current context. */ public function resolveScheme(?string $country = null): ?string { $defaultScheme = (string) ($this->settings['default_scheme'] ?? Scheme::SCHEME_PL); $multiCountry = ! empty($this->settings['enable_destination_matching']); if (! $multiCountry) { return $defaultScheme; } if ($country === null || $country === '') { $country = $this->detectCustomerCountry(); } if ($country === null || $country === '') { return $defaultScheme; } $country = strtoupper(trim($country)); if (isset(self::COUNTRY_MAP[$country])) { return self::COUNTRY_MAP[$country]; } // Export outside supported schemes: check whether export exemption is enabled. $exemptExports = ! empty($this->settings['exempt_unsupported_exports']); if ($exemptExports) { return null; } return $defaultScheme; } /** * Detect shipping country from current WooCommerce session. */ private function detectCustomerCountry(): ?string { if (! function_exists('WC') || WC()->customer === null) { return null; } $shipping = WC()->customer->get_shipping_country(); if (! empty($shipping)) { return (string) $shipping; } $billing = WC()->customer->get_billing_country(); if (! empty($billing)) { return (string) $billing; } return null; } }