currency-switcher
4 weeks ago
class-wcml-custom-prices.php
4 weeks ago
class-wcml-multi-currency-configuration.php
4 weeks ago
class-wcml-multi-currency-coupons.php
1 year ago
class-wcml-multi-currency-install.php
4 weeks ago
class-wcml-multi-currency-orders.php
4 weeks ago
class-wcml-multi-currency-prices.php
4 weeks ago
class-wcml-multi-currency-reports.php
4 weeks ago
class-wcml-multi-currency-resources.php
4 weeks ago
class-wcml-multi-currency-shipping.php
4 weeks ago
class-wcml-multi-currency-table-rate-shipping.php
4 weeks ago
class-wcml-multi-currency.php
4 weeks ago
class-wcml-price-filter.php
4 weeks ago
class-wcml-w3tc-multi-currency.php
4 weeks ago
class-wcml-multi-currency-prices.php
643 lines
| 1 | <?php |
| 2 | |
| 3 | use WCML\COT\Helper as COTHelper; |
| 4 | use WCML\Orders\Helper as OrdersHelper; |
| 5 | use WCML\Orders\Legacy\Helper as LegacyHelper; |
| 6 | use WPML\FP\Fns; |
| 7 | use WPML\FP\Just; |
| 8 | use WPML\FP\Logic; |
| 9 | use WPML\FP\Maybe; |
| 10 | use WPML\FP\Nothing; |
| 11 | use WPML\FP\Obj; |
| 12 | use WPML\FP\Str; |
| 13 | use WPML\LIB\WP\Hooks; |
| 14 | use function WCML\functions\getSitePress; |
| 15 | use function WPML\FP\pipe; |
| 16 | |
| 17 | class WCML_Multi_Currency_Prices { |
| 18 | |
| 19 | const WC_DEFAULT_STEP = 10; |
| 20 | |
| 21 | private $currency_options; |
| 22 | |
| 23 | private $multi_currency; |
| 24 | |
| 25 | private $isSavingPost = false; |
| 26 | |
| 27 | public function __construct( WCML_Multi_Currency $multi_currency, array $currency_options ) { |
| 28 | $this->multi_currency = $multi_currency; |
| 29 | $this->currency_options = $currency_options; |
| 30 | } |
| 31 | |
| 32 | public function add_hooks() { |
| 33 | add_filter( 'wcml_raw_price_amount', [ $this, 'raw_price_filter' ], 10, 2 ); |
| 34 | |
| 35 | add_filter( 'woocommerce_currency', [ $this, 'currency_filter' ] ); |
| 36 | add_filter( 'wcml_price_currency', [ $this, 'price_currency_filter' ] ); |
| 37 | add_filter( 'get_post_metadata', [ $this, 'product_price_filter' ], 10, 4 ); |
| 38 | add_filter( 'wcml_formatted_price', [ $this, 'formatted_price' ], 10, 2 ); |
| 39 | |
| 40 | if ( $this->multi_currency->load_filters ) { |
| 41 | add_filter( |
| 42 | 'wcml_product_price_by_currency', |
| 43 | [ |
| 44 | $this, |
| 45 | 'get_product_price_in_currency', |
| 46 | ], |
| 47 | 10, |
| 48 | 2 |
| 49 | ); |
| 50 | add_filter( 'woocommerce_price_filter_widget_max_amount', [ $this, 'filter_widget_max_amount' ], 99 ); |
| 51 | add_filter( 'woocommerce_price_filter_widget_min_amount', [ $this, 'filter_widget_min_amount' ], 99 ); |
| 52 | |
| 53 | add_filter( 'woocommerce_adjust_price', [ $this, 'raw_price_filter' ], 10 ); |
| 54 | |
| 55 | add_filter( 'woocommerce_paypal_args', [ $this, 'filter_price_woocommerce_paypal_args' ] ); |
| 56 | add_filter( |
| 57 | 'woocommerce_get_variation_prices_hash', |
| 58 | [ |
| 59 | $this, |
| 60 | 'add_currency_to_variation_prices_hash', |
| 61 | ] |
| 62 | ); |
| 63 | add_filter( |
| 64 | 'woocommerce_cart_contents_total', |
| 65 | [ |
| 66 | $this, |
| 67 | 'filter_woocommerce_cart_contents_total', |
| 68 | ], |
| 69 | 100 |
| 70 | ); |
| 71 | add_filter( 'woocommerce_cart_subtotal', [ $this, 'filter_woocommerce_cart_subtotal' ], 100, 3 ); |
| 72 | |
| 73 | add_filter( 'posts_clauses', [ $this, 'price_filter_post_clauses' ], 100, 2 ); |
| 74 | |
| 75 | add_action( |
| 76 | 'woocommerce_cart_loaded_from_session', |
| 77 | [ |
| 78 | $this, |
| 79 | 'filter_currency_num_decimals_in_cart', |
| 80 | ] |
| 81 | ); |
| 82 | |
| 83 | add_filter( 'wc_price_args', [ $this, 'filter_wc_price_args' ] ); |
| 84 | } |
| 85 | |
| 86 | add_filter( 'wc_price_args', [ $this, 'filter_wc_price_args_on_order_admin_screen' ] ); |
| 87 | |
| 88 | add_action( 'woocommerce_cart_loaded_from_session', [ $this, 'recalculate_totals' ], PHP_INT_MAX ); |
| 89 | |
| 90 | add_filter( 'option_woocommerce_price_thousand_sep', [ $this, 'filter_currency_thousand_sep_option' ] ); |
| 91 | add_filter( 'option_woocommerce_price_decimal_sep', [ $this, 'filter_currency_decimal_sep_option' ] ); |
| 92 | add_filter( 'option_woocommerce_price_num_decimals', [ $this, 'filter_currency_num_decimals_option' ] ); |
| 93 | add_filter( 'option_woocommerce_currency_pos', Fns::withoutRecursion( Fns::identity(), [ $this, 'filter_currency_position_option' ] ) ); |
| 94 | |
| 95 | add_filter( 'wpml_pre_save_pro_translation', Fns::tap( [ $this, 'enableSavingPost' ] ) ); |
| 96 | add_action( 'wpml_pro_translation_completed', [ $this, 'disableSavingPost' ] ); |
| 97 | } |
| 98 | |
| 99 | public function enableSavingPost() { |
| 100 | $this->isSavingPost = true; |
| 101 | } |
| 102 | |
| 103 | public function disableSavingPost() { |
| 104 | $this->isSavingPost = false; |
| 105 | } |
| 106 | |
| 107 | public function currency_filter( $currency ) { |
| 108 | |
| 109 | if ( $this->is_multi_currency_filters_loaded() ) { |
| 110 | $currency = apply_filters( 'wcml_price_currency', $currency ); |
| 111 | } |
| 112 | |
| 113 | return $currency; |
| 114 | } |
| 115 | |
| 116 | public function price_currency_filter( $currency ) { |
| 117 | |
| 118 | if ( empty( $currency ) || $this->is_multi_currency_filters_loaded() ) { |
| 119 | $currency = $this->multi_currency->get_client_currency(); |
| 120 | } |
| 121 | |
| 122 | return $currency; |
| 123 | } |
| 124 | |
| 125 | public function filter_widget_min_amount( $min_amount ) { |
| 126 | $step = $this->get_filter_widget_amount_step(); |
| 127 | |
| 128 | return floor( $this->raw_price_filter( $min_amount ) / $step ) * $step; |
| 129 | } |
| 130 | |
| 131 | public function filter_widget_max_amount( $max_price ) { |
| 132 | $step = $this->get_filter_widget_amount_step(); |
| 133 | return ceil( $this->raw_price_filter( $max_price ) / $step ) * $step; |
| 134 | } |
| 135 | |
| 136 | private function get_filter_widget_amount_step() { |
| 137 | return max( apply_filters( 'woocommerce_price_filter_widget_step', self::WC_DEFAULT_STEP ), 1 ); |
| 138 | } |
| 139 | |
| 140 | public function raw_price_filter( $price, $currency = false ) { |
| 141 | |
| 142 | if ( false === $currency ) { |
| 143 | $currency = $this->multi_currency->get_client_currency(); |
| 144 | } |
| 145 | |
| 146 | if ( wcml_get_woocommerce_currency_option() !== $currency ) { |
| 147 | $price = $this->convert_price_amount( $price, $currency ); |
| 148 | $price = $this->apply_rounding_rules( $price, $currency ); |
| 149 | } |
| 150 | |
| 151 | return $price; |
| 152 | |
| 153 | } |
| 154 | |
| 155 | public function get_product_price_in_currency( $product_id, $currency = false ) { |
| 156 | |
| 157 | if ( ! $currency ) { |
| 158 | $currency = $this->multi_currency->get_client_currency(); |
| 159 | } |
| 160 | |
| 161 | remove_filter( |
| 162 | 'get_post_metadata', |
| 163 | [ |
| 164 | $this, |
| 165 | 'product_price_filter', |
| 166 | ], |
| 167 | 10 |
| 168 | ); |
| 169 | |
| 170 | $manual_prices = $this->multi_currency->custom_prices->get_product_custom_prices( $product_id, $currency ); |
| 171 | |
| 172 | if ( $manual_prices && isset( $manual_prices['_price'] ) ) { |
| 173 | |
| 174 | $price = $manual_prices['_price']; |
| 175 | |
| 176 | } else { |
| 177 | |
| 178 | $product = wc_get_product( $product_id ); |
| 179 | $price = $this->raw_price_filter( wc_get_price_including_tax( $product ), $currency ); |
| 180 | |
| 181 | } |
| 182 | |
| 183 | add_filter( |
| 184 | 'get_post_metadata', |
| 185 | [ |
| 186 | $this, |
| 187 | 'product_price_filter', |
| 188 | ], |
| 189 | 10, |
| 190 | 4 |
| 191 | ); |
| 192 | |
| 193 | return $price; |
| 194 | |
| 195 | } |
| 196 | |
| 197 | public function product_price_filter( $null, $object_id, $meta_key, $single ) { |
| 198 | static $unlocked = true; |
| 199 | |
| 200 | if ( |
| 201 | $unlocked |
| 202 | && ! $this->isSavingPost |
| 203 | && $this->is_multi_currency_filters_loaded() |
| 204 | && in_array( get_post_type( $object_id ), [ 'product', 'product_variation' ], true ) |
| 205 | && in_array( $meta_key, wcml_price_custom_fields( $object_id ), true ) |
| 206 | ) { |
| 207 | $unlocked = false; |
| 208 | $currency = $this->multi_currency->get_client_currency(); |
| 209 | |
| 210 | $get_price_by_legacy_ccr = function() use ( $object_id, $meta_key, $single, $currency ) { |
| 211 | $original_object_id = apply_filters( 'wpml_object_id', $object_id, get_post_type( $object_id ), false, getSitePress()->get_default_language() ); |
| 212 | $ccr_rate = Obj::path( [ $meta_key, $currency ], (array) get_post_meta( $original_object_id, '_custom_conversion_rate', true ) ); |
| 213 | |
| 214 | if ( |
| 215 | $ccr_rate |
| 216 | && in_array( $meta_key, [ '_price', '_regular_price', '_sale_price' ], true ) |
| 217 | ) { |
| 218 | $price_original = get_post_meta( $original_object_id, $meta_key, $single ); |
| 219 | if ( is_numeric( $price_original ) ) { |
| 220 | return $price_original * $ccr_rate; |
| 221 | } |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | $get_manual_price = function() use ( $object_id, $meta_key, $currency ) { |
| 226 | return Obj::prop( $meta_key, (array) $this->multi_currency->custom_prices->get_product_custom_prices( $object_id, $currency ) ); |
| 227 | }; |
| 228 | |
| 229 | $get_price_by_auto_conversion = function() use ( $object_id, $meta_key, $single ) { |
| 230 | $price_original = get_post_meta( $object_id, $meta_key, $single ); |
| 231 | if ( is_numeric( $price_original ) ) { |
| 232 | return apply_filters( 'wcml_raw_price_amount', $price_original ); |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | $get_price = Logic::firstSatisfying( |
| 237 | Logic::isNotNull(), |
| 238 | [ |
| 239 | $get_price_by_legacy_ccr, |
| 240 | $get_manual_price, |
| 241 | $get_price_by_auto_conversion, |
| 242 | ] |
| 243 | ); |
| 244 | |
| 245 | $price = $get_price( null ); |
| 246 | |
| 247 | $unlocked = true; |
| 248 | } |
| 249 | |
| 250 | return $price ?? $null; |
| 251 | } |
| 252 | |
| 253 | public function convert_price_amount( $amount, $currency = false ) { |
| 254 | |
| 255 | if ( empty( $currency ) ) { |
| 256 | $currency = $this->multi_currency->get_client_currency(); |
| 257 | } |
| 258 | |
| 259 | if ( wcml_get_woocommerce_currency_option() !== $currency ) { |
| 260 | |
| 261 | $amount = $this->calculate_exchange_rate_price( $amount, $currency, '*' ); |
| 262 | |
| 263 | } |
| 264 | |
| 265 | return $amount; |
| 266 | |
| 267 | } |
| 268 | |
| 269 | public function convert_price_amount_by_currencies( $amount, $from_currency, $to_currency ) { |
| 270 | |
| 271 | if ( wcml_get_woocommerce_currency_option() !== $to_currency ) { |
| 272 | $amount = $this->calculate_exchange_rate_price( $amount, $to_currency, '*' ); |
| 273 | } else { |
| 274 | $amount = $this->calculate_exchange_rate_price( $amount, $from_currency, '/' ); |
| 275 | } |
| 276 | |
| 277 | return $amount; |
| 278 | } |
| 279 | |
| 280 | private function calculate_exchange_rate_price( $amount, $currency, $operator ) { |
| 281 | |
| 282 | $exchange_rates = $this->multi_currency->get_exchange_rates(); |
| 283 | |
| 284 | $initialType = gettype( $amount ); |
| 285 | |
| 286 | if ( isset( $exchange_rates[ $currency ] ) && is_numeric( $amount ) ) { |
| 287 | |
| 288 | if ( '*' === $operator ) { |
| 289 | $amount = $amount * $exchange_rates[ $currency ]; |
| 290 | } elseif ( '/' === $operator ) { |
| 291 | $amount = $amount / $exchange_rates[ $currency ]; |
| 292 | } |
| 293 | |
| 294 | if ( in_array( $currency, $this->multi_currency->get_currencies_without_cents(), true ) ) { |
| 295 | $amount = $this->round_up( $amount ); |
| 296 | } |
| 297 | } else { |
| 298 | $amount = 0; |
| 299 | } |
| 300 | |
| 301 | if ( 'string' === $initialType ) { |
| 302 | $amount = (string) $amount; |
| 303 | } |
| 304 | |
| 305 | return $amount; |
| 306 | |
| 307 | } |
| 308 | |
| 309 | public function unconvert_price_amount( $amount, $currency = false ) { |
| 310 | |
| 311 | if ( empty( $currency ) ) { |
| 312 | $currency = $this->multi_currency->get_client_currency(); |
| 313 | } |
| 314 | |
| 315 | if ( wcml_get_woocommerce_currency_option() !== $currency ) { |
| 316 | |
| 317 | $exchange_rates = $this->multi_currency->get_exchange_rates(); |
| 318 | |
| 319 | if ( isset( $exchange_rates[ $currency ] ) && is_numeric( $amount ) ) { |
| 320 | $amount = $amount / $exchange_rates[ $currency ]; |
| 321 | |
| 322 | if ( in_array( $currency, $this->multi_currency->get_currencies_without_cents(), true ) ) { |
| 323 | $amount = $this->round_up( $amount ); |
| 324 | } |
| 325 | } else { |
| 326 | $amount = 0; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return $amount; |
| 331 | |
| 332 | } |
| 333 | |
| 334 | public function apply_rounding_rules( $price, $currency = false ) { |
| 335 | |
| 336 | if ( empty( $this->currency_options ) ) { |
| 337 | global $woocommerce_wpml; |
| 338 | $this->currency_options = $woocommerce_wpml->get_setting( 'currency_options' ); |
| 339 | } |
| 340 | |
| 341 | if ( ! $currency ) { |
| 342 | $currency = $this->multi_currency->get_client_currency(); |
| 343 | } |
| 344 | |
| 345 | $currency_options = $this->currency_options[ $currency ]; |
| 346 | |
| 347 | if ( 'disabled' !== $currency_options['rounding'] ) { |
| 348 | |
| 349 | if ( $currency_options['rounding_increment'] > 1 ) { |
| 350 | $price = $price / $currency_options['rounding_increment']; |
| 351 | } |
| 352 | |
| 353 | switch ( $currency_options['rounding'] ) { |
| 354 | case 'up': |
| 355 | $rounded_price = ceil( $price ); |
| 356 | break; |
| 357 | case 'down': |
| 358 | $rounded_price = floor( $price ); |
| 359 | break; |
| 360 | case 'nearest': |
| 361 | default: |
| 362 | $rounded_price = $this->round_up( $price ); |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | if ( $rounded_price > 0 ) { |
| 367 | $price = $rounded_price; |
| 368 | } |
| 369 | |
| 370 | if ( $currency_options['rounding_increment'] > 1 ) { |
| 371 | $price = $price * $currency_options['rounding_increment']; |
| 372 | } |
| 373 | |
| 374 | if ( $currency_options['auto_subtract'] && $currency_options['auto_subtract'] < $price ) { |
| 375 | $price = $price - $currency_options['auto_subtract']; |
| 376 | } |
| 377 | } else { |
| 378 | |
| 379 | $price = round( $price, $currency_options['num_decimals'] ); |
| 380 | |
| 381 | } |
| 382 | |
| 383 | return apply_filters( 'wcml_rounded_price', $price, $currency ); |
| 384 | |
| 385 | } |
| 386 | |
| 387 | private function round_up( $amount ): float { |
| 388 | if ( $amount - floor( $amount ) < 0.5 ) { |
| 389 | return floor( $amount ); |
| 390 | } |
| 391 | |
| 392 | return ceil( $amount ); |
| 393 | } |
| 394 | |
| 395 | public function formatted_price( $amount, $currency = false ) { |
| 396 | |
| 397 | if ( false === $currency ) { |
| 398 | $currency = $this->multi_currency->get_client_currency(); |
| 399 | } |
| 400 | |
| 401 | $amount = $this->raw_price_filter( $amount, $currency ); |
| 402 | |
| 403 | return $this->format_price_in_currency( $amount, $currency ); |
| 404 | } |
| 405 | |
| 406 | public function format_price_in_currency( $price, $currency ) { |
| 407 | $currency_details = $this->multi_currency->get_currency_details_by_code( $currency ); |
| 408 | |
| 409 | $wc_price_args = [ |
| 410 | 'currency' => $currency, |
| 411 | 'decimal_separator' => $currency_details['decimal_sep'], |
| 412 | 'thousand_separator' => $currency_details['thousand_sep'], |
| 413 | 'decimals' => $currency_details['num_decimals'], |
| 414 | 'price_format' => $this->get_price_format_in_currency( $currency ), |
| 415 | ]; |
| 416 | |
| 417 | return wc_price( $price, $wc_price_args ); |
| 418 | } |
| 419 | |
| 420 | public function filter_price_woocommerce_paypal_args( $args ) { |
| 421 | |
| 422 | foreach ( $args as $key => $value ) { |
| 423 | if ( substr( $key, 0, 7 ) === 'amount_' ) { |
| 424 | |
| 425 | $currency_details = $this->multi_currency->get_currency_details_by_code( $args['currency_code'] ); |
| 426 | |
| 427 | $args[ $key ] = number_format( $value, $currency_details['num_decimals'], '.', '' ); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | return $args; |
| 432 | } |
| 433 | |
| 434 | public function add_currency_to_variation_prices_hash( $data ) { |
| 435 | |
| 436 | $data['currency'] = $this->multi_currency->get_client_currency(); |
| 437 | $data['exchange_rates_hash'] = md5( wp_json_encode( $this->multi_currency->get_exchange_rates() ) ); |
| 438 | |
| 439 | return $data; |
| 440 | |
| 441 | } |
| 442 | |
| 443 | public function filter_woocommerce_cart_contents_total( $cart_contents_total ) { |
| 444 | remove_filter( |
| 445 | 'woocommerce_cart_contents_total', |
| 446 | [ |
| 447 | $this, |
| 448 | 'filter_woocommerce_cart_contents_total', |
| 449 | ], |
| 450 | 100 |
| 451 | ); |
| 452 | $this->recalculate_totals(); |
| 453 | $cart_contents_total = WC()->cart->get_cart_total(); |
| 454 | add_filter( 'woocommerce_cart_contents_total', [ $this, 'filter_woocommerce_cart_contents_total' ], 100 ); |
| 455 | |
| 456 | return $cart_contents_total; |
| 457 | } |
| 458 | |
| 459 | public function recalculate_totals() { |
| 460 | WC()->cart->calculate_totals(); |
| 461 | } |
| 462 | |
| 463 | public function filter_woocommerce_cart_subtotal( $cart_subtotal, $compound, $cart_object ) { |
| 464 | |
| 465 | remove_filter( 'woocommerce_cart_subtotal', [ $this, 'filter_woocommerce_cart_subtotal' ], 100 ); |
| 466 | |
| 467 | $cart_subtotal = $cart_object->get_cart_subtotal( $compound ); |
| 468 | |
| 469 | add_filter( 'woocommerce_cart_subtotal', [ $this, 'filter_woocommerce_cart_subtotal' ], 100, 3 ); |
| 470 | |
| 471 | return $cart_subtotal; |
| 472 | } |
| 473 | |
| 474 | public function price_filter_post_clauses( $args, $wp_query ) { |
| 475 | |
| 476 | if ( ! $wp_query->is_main_query() || ( ! isset( $_GET['max_price'] ) && ! isset( $_GET['min_price'] ) ) ) { |
| 477 | return $args; |
| 478 | } |
| 479 | |
| 480 | $currency = $this->multi_currency->get_client_currency(); |
| 481 | |
| 482 | if ( $currency !== wcml_get_woocommerce_currency_option() ) { |
| 483 | global $wpdb; |
| 484 | $min_price = isset( $_GET['min_price'] ) ? floatval( wp_unslash( $_GET['min_price'] ) ) : 0; |
| 485 | $max_price = isset( $_GET['max_price'] ) ? floatval( wp_unslash( $_GET['max_price'] ) ) : PHP_INT_MAX; |
| 486 | |
| 487 | $min_price_in_default_currency = $this->unconvert_price_amount( $min_price, $currency ); |
| 488 | $max_price_in_default_currency = $this->unconvert_price_amount( $max_price, $currency ); |
| 489 | |
| 490 | $replaceSinceWc5_1 = Str::replace( |
| 491 | [ $wpdb->prepare( '%f<wc_product_meta_lookup.min_price', $max_price ), $wpdb->prepare( '%f>wc_product_meta_lookup.max_price', $min_price ) ], |
| 492 | [ $wpdb->prepare( '%f<wc_product_meta_lookup.min_price', $max_price_in_default_currency ), $wpdb->prepare( '%f>wc_product_meta_lookup.max_price', $min_price_in_default_currency ) ] |
| 493 | ); |
| 494 | |
| 495 | $replaceBeforeWc5_1 = Str::replace( |
| 496 | [ $wpdb->prepare( 'wc_product_meta_lookup.min_price >= %f', $min_price ), $wpdb->prepare( 'wc_product_meta_lookup.max_price <= %f', $max_price ) ], |
| 497 | [ $wpdb->prepare( 'wc_product_meta_lookup.min_price >= %f', $min_price_in_default_currency ), $wpdb->prepare( 'wc_product_meta_lookup.max_price <= %f', $max_price_in_default_currency ) ] |
| 498 | ); |
| 499 | |
| 500 | return Obj::over( |
| 501 | Obj::lensProp( 'where' ), |
| 502 | pipe( $replaceSinceWc5_1, $replaceBeforeWc5_1 ), |
| 503 | $args |
| 504 | ); |
| 505 | } |
| 506 | |
| 507 | return $args; |
| 508 | } |
| 509 | |
| 510 | public function filter_pre_selected_widget_prices_in_new_currency( $response, $to_currency, $from_currency, $params ) { |
| 511 | |
| 512 | wpml_collect( $params )->each( |
| 513 | function ( $value, $key ) use ( &$response, $from_currency, $to_currency ) { |
| 514 | if ( wpml_collect( [ 'min_price', 'max_price' ] )->contains( $key ) ) { |
| 515 | $response[ $key ] = $this->convert_price_amount( $this->unconvert_price_amount( $value, $from_currency ), $to_currency ); |
| 516 | } |
| 517 | } |
| 518 | ); |
| 519 | |
| 520 | return $response; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | private function get_context_currency_code() { |
| 525 | global $pagenow; |
| 526 | |
| 527 | if ( ( OrdersHelper::isEditingNewOrderItems() || OrdersHelper::isOrderCreateAdminScreen() ) && isset( $_COOKIE['_wcml_order_currency'] ) ) { |
| 528 | $currency_code = $_COOKIE['_wcml_order_currency']; |
| 529 | } elseif ( LegacyHelper::isOrderEditAdminScreen() ) { |
| 530 | $currency_code = OrdersHelper::getCurrency( $_GET['post'], true ); |
| 531 | } elseif ( COTHelper::isOrderEditAdminScreen() && isset( $_GET['id'] ) ) { |
| 532 | $currency_code = OrdersHelper::getCurrency( (int) $_GET['id'], true ); |
| 533 | } elseif ( isset( $_GET['page'] ) && $_GET['page'] == 'wc-reports' && isset( $_COOKIE['_wcml_reports_currency'] ) ) { |
| 534 | $currency_code = $_COOKIE['_wcml_reports_currency']; |
| 535 | } elseif ( isset( $_COOKIE['_wcml_dashboard_currency'] ) && is_admin() && ! defined( 'DOING_AJAX' ) && $pagenow == 'index.php' ) { |
| 536 | $currency_code = $_COOKIE['_wcml_dashboard_currency']; |
| 537 | } else { |
| 538 | $currency_code = $this->multi_currency->get_client_currency(); |
| 539 | } |
| 540 | |
| 541 | return apply_filters( 'wcml_filter_currency_position', $currency_code ); |
| 542 | |
| 543 | } |
| 544 | |
| 545 | public function filter_currency_thousand_sep_option( $value ) { |
| 546 | return $this->filter_currency_option_in_global_secondary_currency( 'thousand_sep', $value ); |
| 547 | } |
| 548 | |
| 549 | public function filter_currency_decimal_sep_option( $value ) { |
| 550 | return $this->filter_currency_option_in_global_secondary_currency( 'decimal_sep', $value ); |
| 551 | } |
| 552 | |
| 553 | public function filter_currency_num_decimals_option( $value ) { |
| 554 | return $this->filter_currency_option_in_global_secondary_currency( 'num_decimals', $value ); |
| 555 | } |
| 556 | |
| 557 | public function filter_currency_position_option( $value ) { |
| 558 | return $this->filter_currency_option_in_global_secondary_currency( 'position', $value ); |
| 559 | } |
| 560 | |
| 561 | private function filter_currency_option_in_global_secondary_currency( $option, $value ) { |
| 562 | $default_currency = $this->multi_currency->get_default_currency(); |
| 563 | $currency_code = $this->get_context_currency_code(); |
| 564 | |
| 565 | if ( $currency_code !== $default_currency ) { |
| 566 | $value = $this->get_currency_option( $currency_code, $option )->getOrElse( $value ); |
| 567 | } |
| 568 | |
| 569 | return $value; |
| 570 | } |
| 571 | |
| 572 | private function get_currency_option( $currency, $option ) { |
| 573 | return Maybe::fromNullable( $this->multi_currency->currencies[ $currency ][ $option ] ?? null ); |
| 574 | } |
| 575 | |
| 576 | public function filter_currency_num_decimals_in_cart( $cart ) { |
| 577 | $cart->dp = wc_get_price_decimals(); |
| 578 | } |
| 579 | |
| 580 | public function filter_wc_price_args_on_order_admin_screen( $args ) { |
| 581 | if ( OrdersHelper::isOrderListAdminScreen() ) { |
| 582 | $args = $this->filter_wc_price_args( $args ); |
| 583 | } |
| 584 | return $args; |
| 585 | } |
| 586 | |
| 587 | public function filter_wc_price_args( $args ) { |
| 588 | $currency = Obj::prop( 'currency', $args ); |
| 589 | |
| 590 | if ( $currency ) { |
| 591 | |
| 592 | foreach ( [ |
| 593 | 'decimal_sep' => 'decimal_sep', |
| 594 | 'thousand_sep' => 'thousand_sep', |
| 595 | 'num_decimals' => 'decimals', |
| 596 | ] as $wcmlOption => $wcOptions ) { |
| 597 | $filteredOption = $this->get_currency_option( $currency, $wcmlOption )->getOrElse( null ); |
| 598 | |
| 599 | if ( $filteredOption ) { |
| 600 | $args[ $wcOptions ] = $filteredOption; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | if ( $this->get_currency_option( $currency, 'position' )->getOrElse( null ) ) { |
| 605 | $args['price_format'] = $this->get_price_format_in_currency( $currency ); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | return $args; |
| 610 | } |
| 611 | |
| 612 | private function get_price_format_in_currency( $currency ) { |
| 613 | $useCurrentCurrencyPos = function( $value ) use ( $currency ) { |
| 614 | return $this->get_currency_option( $currency, 'position' )->getOrElse( $value ); |
| 615 | }; |
| 616 | |
| 617 | return Hooks::callWithFilter( 'get_woocommerce_price_format', 'option_woocommerce_currency_pos', $useCurrentCurrencyPos ); |
| 618 | } |
| 619 | |
| 620 | public function convert_raw_woocommerce_price( $price, $currency = null ) { |
| 621 | if ( null === $currency ) { |
| 622 | $currency = $this->multi_currency->get_client_currency(); |
| 623 | } |
| 624 | |
| 625 | return apply_filters( 'wcml_raw_price_amount', $price, $currency ); |
| 626 | } |
| 627 | |
| 628 | public function get_original_product_price( $value, $product ) { |
| 629 | return get_post_meta( $product->get_id(), '_price', 1 ); |
| 630 | } |
| 631 | |
| 632 | private function is_multi_currency_filters_loaded() { |
| 633 | static $filters_loaded; |
| 634 | |
| 635 | if ( ! $filters_loaded ) { |
| 636 | $filters_loaded = $this->multi_currency->are_filters_need_loading(); |
| 637 | } |
| 638 | |
| 639 | return $filters_loaded; |
| 640 | } |
| 641 | |
| 642 | } |
| 643 |