context = $context; if(!$defaultCurrency) { $defaultCurrency = self::getDefaultWoocomerceCurrency(); } $this->defaultCurrency = $defaultCurrency; $this->currentCurrency = $defaultCurrency; $this->rate = $this->defaultCurrency->getRate(); $this->currencySymbols = self::getDefaultCurrencySymbols(); } /** * @param Currency $currency */ public function setCurrentCurrency($currency) { if ($currency instanceof Currency AND $this->defaultCurrency->getRate()) { $this->currentCurrency = $currency; $this->rate = floatval($this->currentCurrency->getRate() / $this->defaultCurrency->getRate()); } } /** * do not use 'strong' comparison * @see https://www.php.net/manual/en/language.oop5.object-comparison.php * Compare only attributes! * * @return bool */ public function isCurrencyChanged() { return $this->defaultCurrency != $this->currentCurrency; } /** * @return float */ public function getRate() { return $this->rate; } /** * @return Currency */ public function getDefaultCurrency() { return $this->defaultCurrency; } /** * @return Currency */ public function getCurrentCurrency() { return $this->currentCurrency; } /** * @param WC_Product $product * @param string $prop * * @return mixed|null */ protected function getProductProtectedProp($product, $prop) { try { $reflection = new ReflectionClass($product); $property = $reflection->getProperty('data'); if (\PHP_VERSION_ID < 80100) { $property->setAccessible(true); } } catch (ReflectionException $e) { $property = null; } return isset($property->getValue($product)[$prop]) ? $property->getValue($product)[$prop] : null; } /** * @param WC_Product $product * * @return float */ public function getDefaultCurrencyProductPrice($product) { $price = floatval($this->getProductProtectedProp($product, 'price')); if ($this->customProductPriceCallback) { return call_user_func($this->customProductPriceCallback, $price, $product, $this->defaultCurrency); } return $price; } /** * @param WC_Product $product * * @return float|null */ public function getDefaultCurrencyProductSalePrice($product) { $salePrice = $this->getProductProtectedProp($product, 'sale_price'); if ($this->customProductSalePriceCallback) { return call_user_func($this->customProductSalePriceCallback, $salePrice, $product, $this->defaultCurrency); } return $salePrice !== "" ? floatval($salePrice) : null; } /** * @param WC_Product $product * * @return float */ public function getDefaultCurrencyProductRegularPrice($product) { $regularPrice = floatval($this->getProductProtectedProp($product, 'regular_price')); if ($this->customProductRegularPriceCallback) { return call_user_func($this->customProductRegularPriceCallback, $regularPrice, $product, $this->defaultCurrency); } return $regularPrice; } /** * @param WC_Product $product * * @return float */ public function getCurrentCurrencyProductPrice($product) { $price = $this->getDefaultCurrencyProductPrice($product) * $this->rate; if ($this->customProductPriceCallback) { return call_user_func($this->customProductPriceCallback, $price, $product, $this->currentCurrency); } return $price; } /** * @param WC_Product $product * @param float $price * * @return float */ public function getCurrentCurrencyProductPriceWithCustomPrice($product, $price) { $price = $price * $this->rate; if ($this->customProductPriceCallback) { return call_user_func($this->customProductPriceCallback, $price, $product, $this->currentCurrency); } return $price; } /** * @param WC_Product $product * * @return float|null */ public function getCurrentCurrencyProductSalePrice($product) { $salePrice = $this->getDefaultCurrencyProductSalePrice($product); if ($this->customProductSalePriceCallback) { return call_user_func($this->customProductSalePriceCallback, $salePrice, $product, $this->currentCurrency); } return $salePrice !== "" && $salePrice !== null ? $salePrice * $this->rate : null; } /** * @param WC_Product $product * * @return float */ public function getCurrentCurrencyProductRegularPrice($product) { $regularPrice = $this->getDefaultCurrencyProductRegularPrice($product) * $this->rate; if ($this->customProductRegularPriceCallback) { return call_user_func($this->customProductRegularPriceCallback, $regularPrice, $product, $this->currentCurrency); } return $regularPrice; } /** * @param array $currencySymbols */ public function setCurrencySymbols($currencySymbols) { if ( ! empty($currencySymbols) && is_array($currencySymbols)) { $this->currencySymbols = $currencySymbols; } } /** * @return array */ public function getCurrencySymbols() { return $this->currencySymbols; } /** * @param callable $callback */ public function setCustomProductSalePriceCallback($callback) { if (is_callable($callback)) { $this->customProductSalePriceCallback = $callback; } } /** * @param callable $callback */ public function setCustomProductRegularPriceCallback($callback) { if (is_callable($callback)) { $this->customProductRegularPriceCallback = $callback; } } /** * @param callable|null $callback */ public function setCustomProductPriceCallback($callback) { if (is_callable($callback)) { $this->customProductPriceCallback = $callback; } } /** * @return array */ public static function getDefaultCurrencySymbols() { return array( 'AED' => 'د.إ', 'AFN' => '؋', 'ALL' => 'L', 'AMD' => 'AMD', 'ANG' => 'ƒ', 'AOA' => 'Kz', 'ARS' => '$', 'AUD' => '$', 'AWG' => 'Afl.', 'AZN' => 'AZN', 'BAM' => 'KM', 'BBD' => '$', 'BDT' => '৳ ', 'BGN' => 'лв.', 'BHD' => '.د.ب', 'BIF' => 'Fr', 'BMD' => '$', 'BND' => '$', 'BOB' => 'Bs.', 'BRL' => 'R$', 'BSD' => '$', 'BTC' => '฿', 'BTN' => 'Nu.', 'BWP' => 'P', 'BYR' => 'Br', 'BYN' => 'Br', 'BZD' => '$', 'CAD' => '$', 'CDF' => 'Fr', 'CHF' => 'CHF', 'CLP' => '$', 'CNY' => '¥', 'COP' => '$', 'CRC' => '₡', 'CUC' => '$', 'CUP' => '$', 'CVE' => '$', 'CZK' => 'Kč', 'DJF' => 'Fr', 'DKK' => 'DKK', 'DOP' => 'RD$', 'DZD' => 'د.ج', 'EGP' => 'EGP', 'ERN' => 'Nfk', 'ETB' => 'Br', 'EUR' => '€', 'FJD' => '$', 'FKP' => '£', 'GBP' => '£', 'GEL' => '₾', 'GGP' => '£', 'GHS' => '₵', 'GIP' => '£', 'GMD' => 'D', 'GNF' => 'Fr', 'GTQ' => 'Q', 'GYD' => '$', 'HKD' => '$', 'HNL' => 'L', 'HRK' => 'kn', 'HTG' => 'G', 'HUF' => 'Ft', 'IDR' => 'Rp', 'ILS' => '₪', 'IMP' => '£', 'INR' => '₹', 'IQD' => 'ع.د', 'IRR' => '﷼', 'IRT' => 'تومان', 'ISK' => 'kr.', 'JEP' => '£', 'JMD' => '$', 'JOD' => 'د.ا', 'JPY' => '¥', 'KES' => 'KSh', 'KGS' => 'сом', 'KHR' => '៛', 'KMF' => 'Fr', 'KPW' => '₩', 'KRW' => '₩', 'KWD' => 'د.ك', 'KYD' => '$', 'KZT' => 'KZT', 'LAK' => '₭', 'LBP' => 'ل.ل', 'LKR' => 'රු', 'LRD' => '$', 'LSL' => 'L', 'LYD' => 'ل.د', 'MAD' => 'د.م.', 'MDL' => 'MDL', 'MGA' => 'Ar', 'MKD' => 'ден', 'MMK' => 'Ks', 'MNT' => '₮', 'MOP' => 'P', 'MRU' => 'UM', 'MUR' => '₨', 'MVR' => '.ރ', 'MWK' => 'MK', 'MXN' => '$', 'MYR' => 'RM', 'MZN' => 'MT', 'NAD' => 'N$', 'NGN' => '₦', 'NIO' => 'C$', 'NOK' => 'kr', 'NPR' => '₨', 'NZD' => '$', 'OMR' => 'ر.ع.', 'PAB' => 'B/.', 'PEN' => 'S/', 'PGK' => 'K', 'PHP' => '₱', 'PKR' => '₨', 'PLN' => 'zł', 'PRB' => 'р.', 'PYG' => '₲', 'QAR' => 'ر.ق', 'RMB' => '¥', 'RON' => 'lei', 'RSD' => 'дин.', 'RUB' => '₽', 'RWF' => 'Fr', 'SAR' => 'ر.س', 'SBD' => '$', 'SCR' => '₨', 'SDG' => 'ج.س.', 'SEK' => 'kr', 'SGD' => '$', 'SHP' => '£', 'SLL' => 'Le', 'SOS' => 'Sh', 'SRD' => '$', 'SSP' => '£', 'STN' => 'Db', 'SYP' => 'ل.س', 'SZL' => 'L', 'THB' => '฿', 'TJS' => 'ЅМ', 'TMT' => 'm', 'TND' => 'د.ت', 'TOP' => 'T$', 'TRY' => '₺', 'TTD' => '$', 'TWD' => 'NT$', 'TZS' => 'Sh', 'UAH' => '₴', 'UGX' => 'UGX', 'USD' => '$', 'UYU' => '$', 'UZS' => 'UZS', 'VEF' => 'Bs F', 'VES' => 'Bs.S', 'VND' => '₫', 'VUV' => 'Vt', 'WST' => 'T', 'XAF' => 'CFA', 'XCD' => '$', 'XOF' => 'CFA', 'XPF' => 'Fr', 'YER' => '﷼', 'ZAR' => 'R', 'ZMW' => 'ZK', ); } public static function getDefaultWoocomerceCurrency() { $currencyCode = get_option('woocommerce_currency'); $symbols = self::getDefaultCurrencySymbols(); $symbol = isset($symbols[$currencyCode]) ? $symbols[$currencyCode] : ''; return new Currency($currencyCode, $symbol, 1); } }