| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- declare(strict_types=1);
- namespace EKSRelay\Core;
- /**
- * Maps WPML language codes to ISO 4217 currency codes.
- *
- * Used to resolve the correct shop currency based on the detected language
- * of the customer's message. Handles fallback to EUR for currencies that are
- * not configured in the shop's multicurrency plugin.
- *
- * Supported currencies are read from the STORE_CURRENCIES env variable
- * (comma-separated ISO 4217 codes). Default matches the shop configuration.
- */
- final class StoreLocales
- {
- /**
- * WPML language code → natural ISO 4217 currency code.
- * Based on the official currency of the country/region for each language.
- */
- private const CURRENCY_MAP = [
- 'aed' => 'AED', // Arabic (UAE) → UAE Dirham
- 'be' => 'BYN', // Belarusian → Belarusian ruble (not in shop → fallback)
- 'bg' => 'BGN', // Bulgarian → Bulgarian lev
- 'hr' => 'EUR', // Croatian → Euro (adopted 2023)
- 'cs' => 'CZK', // Czech → Czech koruna
- 'da' => 'DKK', // Danish → Danish krone
- 'nl' => 'EUR', // Dutch → Euro
- 'en' => 'EUR', // English → Euro (default for EU context)
- 'et' => 'EUR', // Estonian → Euro
- 'fi' => 'EUR', // Finnish → Euro
- 'fr' => 'EUR', // French → Euro
- 'de' => 'EUR', // German → Euro
- 'el' => 'EUR', // Greek → Euro
- 'hu' => 'HUF', // Hungarian → Hungarian forint
- 'it' => 'EUR', // Italian → Euro
- 'lv' => 'EUR', // Latvian → Euro
- 'lt' => 'EUR', // Lithuanian → Euro
- 'no' => 'NOK', // Norwegian → Norwegian krone
- 'pl' => 'PLN', // Polish → Polish złoty
- 'pt-pt' => 'EUR', // Portuguese (Portugal) → Euro
- 'ro' => 'RON', // Romanian → Romanian leu
- 'sk' => 'EUR', // Slovak → Euro
- 'sl' => 'EUR', // Slovenian → Euro
- 'es' => 'EUR', // Spanish → Euro
- 'sv' => 'SEK', // Swedish → Swedish krona
- 'tr' => 'TRY', // Turkish → Turkish lira (not in shop → fallback)
- 'uk' => 'UAH', // Ukrainian → Ukrainian hryvnia (not in shop → fallback)
- ];
- /** Fallback currency used when the natural currency is not supported. */
- public const FALLBACK_CURRENCY = 'EUR';
- /**
- * Default list of currencies configured in the shop's multicurrency plugin.
- * Used when STORE_CURRENCIES env variable is not set.
- * Keep in sync with WooCommerce multicurrency configuration.
- */
- private const DEFAULT_SUPPORTED = 'PLN,EUR,AED,CZK,HUF,DKK,SEK,NOK,RON,BGN,GBP';
- /**
- * Resolve a WPML language code to a shop-supported ISO 4217 currency.
- *
- * Returns an array with:
- * currency — ISO 4217 code to pass to the WooCommerce API
- * fallback — true if we had to fall back from the natural currency
- * natural_currency — (only when fallback=true) the natural currency for the language
- * reason — (only when fallback=true) 'unknown_language' | 'unsupported_currency'
- *
- * @param string|null $language WPML language code (e.g. 'de', 'pl', 'pt-pt')
- */
- public static function resolve(?string $language): array
- {
- if ($language === null || $language === '') {
- return ['currency' => self::FALLBACK_CURRENCY, 'fallback' => false];
- }
- $lang = strtolower(trim($language));
- $natural = self::CURRENCY_MAP[$lang] ?? null;
- if ($natural === null) {
- return [
- 'currency' => self::FALLBACK_CURRENCY,
- 'fallback' => true,
- 'reason' => 'unknown_language',
- ];
- }
- if (!in_array($natural, self::supportedCurrencies(), true)) {
- return [
- 'currency' => self::FALLBACK_CURRENCY,
- 'fallback' => true,
- 'reason' => 'unsupported_currency',
- 'natural_currency' => $natural,
- ];
- }
- return ['currency' => $natural, 'fallback' => false];
- }
- /**
- * Check whether a currency code is supported by the shop's multicurrency configuration.
- *
- * @param string $currency ISO 4217 code (comparison is case-insensitive)
- */
- public static function isSupported(string $currency): bool
- {
- return in_array(strtoupper($currency), self::supportedCurrencies(), true);
- }
- /**
- * @return string[]
- */
- private static function supportedCurrencies(): array
- {
- $raw = Env::get('STORE_CURRENCIES', self::DEFAULT_SUPPORTED);
- return array_values(array_filter(array_map('trim', explode(',', strtoupper($raw)))));
- }
- }
|