decimals(); } /** * The store's base currency code. Read from the option, not * get_woocommerce_currency(), which multi-currency plugins filter to the * visitor's currency. */ public function base(): string { return (string) get_option( 'woocommerce_currency', '' ); } /** * Minor units of an order's currency in the base currency, or null when * the order has no usable rate. * * @param int $minor Amount in the order's minor units. * @param \WC_Order $order The order. */ public function to_base( int $minor, \WC_Order $order ): ?int { $rate = $this->rate( $order ); if ( null === $rate ) { return null; } return 1.0 === $rate ? $minor : (int) round( $minor / $rate ); } /** * Whether an order's amounts cannot be expressed in the base currency. * * @param \WC_Order $order The order. */ public function unconvertible( \WC_Order $order ): bool { return null === $this->rate( $order ); } /** * Order-currency units per base unit: 1 for the base currency, the * stored rate otherwise, null without a positive one. * * @param \WC_Order $order The order. */ private function rate( \WC_Order $order ): ?float { if ( $order->get_currency() === $this->base() ) { return 1.0; } $stored = $order->get_meta( self::WCPAY_RATE_META ); $rate = is_numeric( $stored ) ? (float) $stored : null; /** * Filters the exchange rate stored on an order in a currency other * than the store's base currency: order-currency units per one base * unit. Return null when the order has none; it is then held rather * than measured. * * @param float|null $rate The rate found, or null. * @param \WC_Order $order The order. */ $rate = apply_filters( 'adafwc_order_exchange_rate', $rate, $order ); return ( is_float( $rate ) || is_int( $rate ) ) && is_finite( (float) $rate ) && $rate > 0 ? (float) $rate : null; } /** * The store's price decimals setting. */ private static function store_decimals(): int { return (int) wc_get_price_decimals(); } }