WCML_Multi_Currency_Prices

The WooCommerce Multilingual WCML Multi Currency Prices class.

Defined (1)

The class is defined in the following location(s).

/inc/currencies/class-wcml-multi-currency-prices.php  
  1. class WCML_Multi_Currency_Prices{ 
  2.  
  3. /** 
  4. * @var woocommerce_wpml 
  5. */ 
  6. private $woocommerce_wpml; 
  7. /** 
  8. * @var WCML_Multi_Currency 
  9. */ 
  10. private $multi_currency; 
  11. /** 
  12. * @var orders_list_currency 
  13. */ 
  14. private $orders_list_currency; 
  15.  
  16. public function __construct( &$multi_currency ) { 
  17.  
  18. $this->multi_currency =& $multi_currency; 
  19.  
  20. if( $this->multi_currency->load_filters ) { 
  21. add_filter('init', array($this, 'prices_init'), 5); 
  22.  
  23. // Currency and Amount filters 
  24. add_filter('woocommerce_currency', array($this, 'currency_filter')); 
  25.  
  26. add_filter( 'wcml_price_currency', array($this, 'price_currency_filter') ); // WCML filters 
  27. add_filter( 'wcml_raw_price_amount', array($this, 'raw_price_filter'), 10, 2 ); // WCML filters 
  28. add_filter( 'wcml_product_price_by_currency', array($this, 'get_product_price_in_currency'), 10, 2 ); // WCML filters 
  29.  
  30. add_filter('get_post_metadata', array($this, 'product_price_filter'), 10, 4); 
  31. add_filter('get_post_metadata', array($this, 'variation_prices_filter'), 12, 4); // second 
  32.  
  33. add_filter('woocommerce_price_filter_widget_max_amount', array($this, 'raw_price_filter'), 99); 
  34. add_filter('woocommerce_price_filter_widget_min_amount', array($this, 'raw_price_filter'), 99); 
  35.  
  36. add_filter('woocommerce_adjust_price', array($this, 'raw_price_filter'), 10); 
  37.  
  38. add_filter('wcml_formatted_price', array($this, 'formatted_price'), 10, 2); // WCML filters 
  39.  
  40. // Shipping prices 
  41. add_filter('woocommerce_paypal_args', array($this, 'filter_price_woocommerce_paypal_args')); 
  42. add_filter('woocommerce_get_variation_prices_hash', array($this, 'add_currency_to_variation_prices_hash')); 
  43. add_filter('woocommerce_cart_contents_total', array($this, 'filter_woocommerce_cart_contents_total'), 100); 
  44. add_filter('woocommerce_cart_subtotal', array($this, 'filter_woocommerce_cart_subtotal'), 100, 3); 
  45.  
  46. //filters for wc-widget-price-filter 
  47. add_filter('woocommerce_price_filter_results', array($this, 'filter_price_filter_results'), 10, 3); 
  48. add_filter('woocommerce_price_filter_widget_amount', array($this, 'filter_price_filter_widget_amount')); 
  49.  
  50. add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'filter_currency_num_decimals_in_cart' ) ); 
  51.  
  52. if( version_compare( WOOCOMMERCE_VERSION, '2.3', '<' ) ) { 
  53. add_filter( 'wc_price', array( $this, 'price_in_specific_currency' ), 10, 3 ); 
  54.  
  55. add_filter( 'wc_price_args', array( $this, 'filter_wc_price_args') ); 
  56.  
  57. WCML_Multi_Currency_Table_Rate_Shipping::set_up(); 
  58.  
  59. // formatting options 
  60. add_filter('option_woocommerce_price_thousand_sep', array($this, 'filter_currency_thousand_sep_option')); 
  61. add_filter('option_woocommerce_price_decimal_sep', array($this, 'filter_currency_decimal_sep_option')); 
  62. add_filter('option_woocommerce_price_num_decimals', array($this, 'filter_currency_num_decimals_option')); 
  63. add_filter('option_woocommerce_currency_pos', array($this, 'filter_currency_position_option')); 
  64.  
  65. //need for display correct price format for order on orders list page 
  66. add_filter( 'get_post_metadata', array( $this, 'save_order_currency_for_filter' ), 10, 4); 
  67.  
  68.  
  69. public function prices_init() { 
  70. global $woocommerce_wpml; 
  71. $this->woocommerce_wpml =& $woocommerce_wpml; 
  72.  
  73.  
  74. public function currency_filter($currency) { 
  75.  
  76. $currency = apply_filters('wcml_price_currency', $currency); 
  77.  
  78. return $currency; 
  79.  
  80. public function price_currency_filter( $currency ) { 
  81.  
  82. if(isset($this->order_currency)) { 
  83. $currency = $this->order_currency; 
  84. }else{ 
  85. $currency = $this->multi_currency->get_client_currency(); 
  86.  
  87. return $currency; 
  88.  
  89. public function raw_price_filter($price, $currency = false) { 
  90.  
  91. if( $currency === false ) { 
  92. $currency = $this->multi_currency->get_client_currency(); 
  93.  
  94. if( $currency != get_option('woocommerce_currency')) { 
  95. $price = $this->convert_price_amount( $price, $currency ); 
  96. $price = $this->apply_rounding_rules( $price, $currency ); 
  97.  
  98. return $price; 
  99.  
  100.  
  101. public function get_product_price_in_currency( $product_id, $currency = false ) { 
  102.  
  103. if( !$currency ) { 
  104. $currency = $this->multi_currency->get_client_currency(); 
  105.  
  106. remove_filter( 'get_post_metadata', array( $this->woocommerce_wpml->multi_currency->prices, 'product_price_filter' ), 10, 4 ); 
  107.  
  108. $manual_prices = $this->multi_currency->custom_prices->get_product_custom_prices( $product_id, $currency ); 
  109.  
  110. if( $manual_prices && !empty( $manual_prices[ '_price' ] ) ) { 
  111.  
  112. $price = $manual_prices[ '_price' ]; 
  113.  
  114. }else{ 
  115.  
  116. $product = wc_get_product( $product_id ); 
  117. $price = $this->raw_price_filter( $product->get_price(), $currency ); 
  118.  
  119.  
  120. add_filter( 'get_post_metadata', array( $this->woocommerce_wpml->multi_currency->prices, 'product_price_filter' ), 10, 4 ); 
  121.  
  122. return $price; 
  123.  
  124.  
  125. public function product_price_filter($null, $object_id, $meta_key, $single) { 
  126. global $sitepress; 
  127.  
  128. static $no_filter = false; 
  129.  
  130. if(empty($no_filter) && in_array(get_post_type($object_id), array('product', 'product_variation'))) { 
  131.  
  132. $price_keys = apply_filters( 'wcml_price_custom_fields_filtered', array( 
  133. '_price',  
  134. '_regular_price',  
  135. '_sale_price',  
  136. '_min_variation_price',  
  137. '_max_variation_price',  
  138. '_min_variation_regular_price',  
  139. '_max_variation_regular_price',  
  140. '_min_variation_sale_price',  
  141. '_max_variation_sale_price' 
  142. ) ); 
  143.  
  144. if(in_array($meta_key, $price_keys)) { 
  145. $no_filter = true; 
  146.  
  147. // exception for products migrated from before WCML 3.1 with independent prices 
  148. // legacy prior 3.1 
  149. $original_object_id = apply_filters( 'translate_object_id', $object_id, get_post_type($object_id), false, $sitepress->get_default_language()); 
  150. $ccr = get_post_meta($original_object_id, '_custom_conversion_rate', true); 
  151. if(in_array($meta_key, array('_price', '_regular_price', '_sale_price')) && !empty($ccr) && isset($ccr[$meta_key][$this->multi_currency->get_client_currency()])) { 
  152. $price_original = get_post_meta($original_object_id, $meta_key, $single); 
  153. $price = $price_original * $ccr[$meta_key][$this->multi_currency->get_client_currency()]; 
  154.  
  155. }else{ 
  156.  
  157. // normal filtering 
  158. // 1. manual prices 
  159. $manual_prices = $this->multi_currency->custom_prices->get_product_custom_prices($object_id, $this->multi_currency->get_client_currency()); 
  160.  
  161. if($manual_prices && !empty($manual_prices[$meta_key])) { 
  162.  
  163. $price = $manual_prices[$meta_key]; 
  164.  
  165. }else{ 
  166. // 2. automatic conversion 
  167. $price = get_post_meta($object_id, $meta_key, $single); 
  168. $price = apply_filters('wcml_raw_price_amount', $price ); 
  169.  
  170.  
  171.  
  172.  
  173. $no_filter = false; 
  174.  
  175.  
  176. return !empty($price) ? $price : $null; 
  177.  
  178. public function variation_prices_filter($null, $object_id, $meta_key, $single) { 
  179.  
  180. if(empty($meta_key) && get_post_type($object_id) == 'product_variation') { 
  181. static $no_filter = false; 
  182.  
  183. if(empty($no_filter)) { 
  184. $no_filter = true; 
  185.  
  186. $variation_fields = get_post_meta($object_id); 
  187.  
  188. $manual_prices = $this->multi_currency->custom_prices->get_product_custom_prices($object_id, $this->multi_currency->get_client_currency()); 
  189.  
  190. foreach($variation_fields as $k => $v) { 
  191.  
  192. if(in_array($k, array('_price', '_regular_price', '_sale_price'))) { 
  193.  
  194. foreach($v as $j => $amount) { 
  195.  
  196. if(isset($manual_prices[$k])) { 
  197. $variation_fields[$k][$j] = $manual_prices[$k]; // manual price 
  198.  
  199. }else{ 
  200. $variation_fields[$k][$j] = apply_filters('wcml_raw_price_amount', $amount ); // automatic conversion 
  201.  
  202.  
  203.  
  204.  
  205. $no_filter = false; 
  206.  
  207.  
  208. return !empty($variation_fields) ? $variation_fields : $null; 
  209.  
  210.  
  211. public function convert_price_amount($amount, $currency = false) { 
  212.  
  213. if( empty( $currency ) ) { 
  214. $currency = $this->multi_currency->get_client_currency(); 
  215.  
  216. if( $currency != get_option('woocommerce_currency')) { 
  217.  
  218. $exchange_rates = $this->multi_currency->get_exchange_rates(); 
  219.  
  220. if(isset($exchange_rates[$currency]) && is_numeric($amount)) { 
  221. $amount = $amount * $exchange_rates[$currency]; 
  222.  
  223. // exception - currencies_without_cents 
  224. if(in_array($currency, $this->multi_currency->get_currencies_without_cents())) { 
  225. $amount = $this->round_up( $amount ); 
  226.  
  227. }else{ 
  228. $amount = 0; 
  229.  
  230.  
  231. return $amount; 
  232.  
  233.  
  234. // convert back to default currency 
  235. public function unconvert_price_amount($amount, $currency = false) { 
  236.  
  237. if(empty($currency)) { 
  238. $currency = $this->multi_currency->get_client_currency(); 
  239.  
  240. if($currency != get_option('woocommerce_currency')) { 
  241.  
  242. $exchange_rates = $this->multi_currency->get_exchange_rates(); 
  243.  
  244. if(isset($exchange_rates[$currency]) && is_numeric($amount)) { 
  245. $amount = $amount / $exchange_rates[$currency]; 
  246.  
  247. // exception - currencies_without_cents 
  248. if(in_array($currency, $this->multi_currency->get_currencies_without_cents())) { 
  249. $amount = $this->round_up( $amount ); 
  250.  
  251. }else{ 
  252. $amount = 0; 
  253.  
  254.  
  255. return $amount; 
  256.  
  257.  
  258. public function apply_rounding_rules($price, $currency = false ) { 
  259.  
  260. if( is_null($this->woocommerce_wpml) ) { 
  261. global $woocommerce_wpml; 
  262. $this->woocommerce_wpml = $woocommerce_wpml; 
  263.  
  264. if( !$currency ) { 
  265. $currency = $this->multi_currency->get_client_currency(); 
  266.  
  267. $currency_options = $this->woocommerce_wpml->settings['currency_options'][$currency]; 
  268.  
  269. if( $currency_options['rounding'] != 'disabled' ) { 
  270.  
  271. if($currency_options['rounding_increment'] > 1) { 
  272. $price = $price / $currency_options['rounding_increment']; 
  273.  
  274. switch($currency_options['rounding']) { 
  275. case 'up': 
  276. $rounded_price = ceil($price); 
  277. break; 
  278. case 'down': 
  279. $rounded_price = floor($price); 
  280. break; 
  281. case 'nearest': 
  282. $rounded_price = $this->round_up( $price ); 
  283. break; 
  284.  
  285. if($rounded_price > 0) { 
  286. $price = $rounded_price; 
  287.  
  288. if($currency_options['rounding_increment'] > 1) { 
  289. $price = $price * $currency_options['rounding_increment']; 
  290.  
  291. if($currency_options['auto_subtract'] && $currency_options['auto_subtract'] < $price) { 
  292. $price = $price - $currency_options['auto_subtract']; 
  293.  
  294. } else { 
  295.  
  296. // Use configured number of decimals 
  297. $price = floor( $price * pow( 10, $currency_options['num_decimals']) + 0.0001 ) / pow( 10, $currency_options['num_decimals'] ); 
  298.  
  299.  
  300.  
  301. return apply_filters( 'wcml_rounded_price', $price, $currency ); 
  302.  
  303.  
  304. /** 
  305. * The PHP 5.2 compatible equivalent to "round($amount, 0, PHP_ROUND_HALF_UP)" 
  306. * @param int $amount 
  307. * @return int 
  308. */ 
  309. private function round_up( $amount ) { 
  310. if( $amount - floor( $amount ) < 0.5 ) { 
  311. $amount = floor( $amount ); 
  312. }else{ 
  313. $amount = ceil( $amount ); 
  314. return $amount; 
  315.  
  316. /** 
  317. * Converts the price from the default currency to the given currency and applies the format 
  318. */ 
  319. public function formatted_price($amount, $currency = false) { 
  320.  
  321. if( $currency === false ) { 
  322. $currency = $this->multi_currency->get_client_currency(); 
  323.  
  324. $amount = $this->raw_price_filter($amount, $currency); 
  325.  
  326. $currency_details = $this->woocommerce_wpml->multi_currency->get_currency_details_by_code( $currency ); 
  327.  
  328. switch ( $currency_details[ 'position' ] ) { 
  329. case 'left' : 
  330. $format = '%1$s%2$s'; 
  331. break; 
  332. case 'right' : 
  333. $format = '%2$s%1$s'; 
  334. break; 
  335. case 'left_space' : 
  336. $format = '%1$s %2$s'; 
  337. break; 
  338. case 'right_space' : 
  339. $format = '%2$s %1$s'; 
  340. break; 
  341.  
  342. $wc_price_args = array( 
  343.  
  344. 'currency' => $currency,  
  345. 'decimal_separator' => $currency_details['decimal_sep'],  
  346. 'thousand_separator' => $currency_details['thousand_sep'],  
  347. 'decimals' => $currency_details['num_decimals'],  
  348. 'price_format' => $format,  
  349.  
  350.  
  351. ); 
  352.  
  353. $price = wc_price($amount, $wc_price_args); 
  354.  
  355. return $price; 
  356.  
  357. // Exposed function 
  358. public function apply_currency_position( $price, $currency_code ) { 
  359.  
  360. $currencies = $this->woocommerce_wpml->multi_currency->get_currencies(); 
  361.  
  362. if( isset( $currencies[$currency_code]['position'] ) ) { 
  363. $position = $currencies[$currency_code]['position']; 
  364. }else{ 
  365. remove_filter( 'option_woocommerce_currency_pos', array( $this->woocommerce_wpml->multi_currency->prices, 'filter_currency_position_option' ) ); 
  366. $position = get_option('woocommerce_currency_pos'); 
  367. add_filter( 'option_woocommerce_currency_pos', array( $this->woocommerce_wpml->multi_currency->prices, 'filter_currency_position_option' ) ); 
  368.  
  369. switch( $position ) { 
  370. case 'left': $price = sprintf( '%s%s', get_woocommerce_currency_symbol( $currency_code ), $price ); break; 
  371. case 'right': $price = sprintf( '%s%s', $price, get_woocommerce_currency_symbol( $currency_code ) ); break; 
  372. case 'left_space': $price = sprintf( '%s %s', get_woocommerce_currency_symbol( $currency_code ), $price ); break; 
  373. case 'right_space': $price = sprintf( '%s %s', $price, get_woocommerce_currency_symbol( $currency_code ) ); break; 
  374.  
  375. return $price; 
  376.  
  377. public function filter_price_woocommerce_paypal_args( $args ) { 
  378.  
  379. foreach( $args as $key => $value ) { 
  380. if( substr( $key, 0, 7 ) == 'amount_' ) { 
  381.  
  382. $currency_details = $this->woocommerce_wpml->multi_currency->get_currency_details_by_code( $args['currency_code'] ); 
  383.  
  384. $args[ $key ] = number_format( $value, $currency_details['num_decimals'], '.', '' ); 
  385.  
  386. return $args; 
  387.  
  388. public function add_currency_to_variation_prices_hash($data) { 
  389.  
  390. $data['currency'] = $this->multi_currency->get_client_currency(); 
  391. $data['exchange_rates_hash'] = md5( json_encode( $this->multi_currency->get_exchange_rates() ) ); 
  392.  
  393. return $data; 
  394.  
  395.  
  396. public function filter_woocommerce_cart_contents_total( $cart_contents_total ) { 
  397. global $woocommerce; 
  398. remove_filter( 'woocommerce_cart_contents_total', array( $this, 'filter_woocommerce_cart_contents_total'), 100 ); 
  399. $woocommerce->cart->calculate_totals(); 
  400. $cart_contents_total = $woocommerce->cart->get_cart_total(); 
  401. add_filter( 'woocommerce_cart_contents_total', array( $this, 'filter_woocommerce_cart_contents_total'), 100 ); 
  402.  
  403. return $cart_contents_total; 
  404.  
  405. public function filter_woocommerce_cart_subtotal( $cart_subtotal, $compound, $obj ) { 
  406. global $woocommerce; 
  407. remove_filter( 'woocommerce_cart_subtotal', array( $this, 'filter_woocommerce_cart_subtotal'), 100, 3 ); 
  408. if( apply_filters( 'wcml_calculate_totals_exception', true ) ) { 
  409. $woocommerce->cart->calculate_totals(); 
  410. $cart_subtotal = $woocommerce->cart->get_cart_subtotal( $compound ); 
  411. add_filter( 'woocommerce_cart_subtotal', array( $this, 'filter_woocommerce_cart_subtotal'), 100, 3 ); 
  412.  
  413. return $cart_subtotal; 
  414.  
  415. public function filter_price_filter_results( $matched_products, $min, $max ) { 
  416. global $wpdb; 
  417.  
  418. $current_currency = $this->multi_currency->get_client_currency(); 
  419. if( $current_currency != get_option('woocommerce_currency') ) { 
  420. $filtered_min = $this->unconvert_price_amount( $min, $current_currency ); 
  421. $filtered_max = $this->unconvert_price_amount( $max, $current_currency ); 
  422.  
  423. $matched_products = $wpdb->get_results( $wpdb->prepare(" 
  424. SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts 
  425. INNER JOIN $wpdb->postmeta ON ID = post_id 
  426. WHERE post_type IN ( 'product', 'product_variation' ) AND post_status = 'publish' AND meta_key = %s AND meta_value BETWEEN %d AND %d 
  427. ", '_price', $filtered_min, $filtered_max ), OBJECT_K ); 
  428.  
  429. foreach( $matched_products as $key => $matched_product ) { 
  430. $custom_price = get_post_meta( $matched_product->ID, '_price_'.$current_currency, true ); 
  431. if( $custom_price && ( $custom_price < $min || $custom_price > $max ) ) { 
  432. unset( $matched_products[$key] ); 
  433.  
  434. return $matched_products; 
  435.  
  436. public function filter_price_filter_widget_amount( $amount ) { 
  437.  
  438. $current_currency = $this->multi_currency->get_client_currency(); 
  439. if( $current_currency != get_option('woocommerce_currency') ) { 
  440. $amount = apply_filters('wcml_raw_price_amount', $amount ); 
  441.  
  442. return $amount; 
  443.  
  444.  
  445. private function check_admin_order_currency_code() 
  446. global $pagenow; 
  447.  
  448. $actions = array('woocommerce_add_order_item', 'woocommerce_save_order_items', 'woocommerce_calc_line_taxes'); 
  449. $is_ajax_order_action = 
  450. is_ajax() && 
  451. isset($_POST['action']) && 
  452. in_array($_POST['action'], $actions) || 
  453. isset($_GET['action']) && 
  454. $_GET['action'] == 'woocommerce_json_search_products_and_variations' 
  455. ); 
  456.  
  457. $is_shop_order_new = $pagenow == 'post-new.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'shop_order'; 
  458.  
  459. if (($is_ajax_order_action || $is_shop_order_new) && isset($_COOKIE['_wcml_order_currency'])) { 
  460. $currency_code = $_COOKIE['_wcml_order_currency']; 
  461. } elseif (isset($_GET['post']) && get_post_type($_GET['post']) == 'shop_order') { 
  462. $currency_code = get_post_meta($_GET['post'], '_order_currency', true); 
  463. }elseif( isset( $_GET[ 'post_type' ] ) && $_GET[ 'post_type' ] == 'shop_order' && !is_null( $this->orders_list_currency ) ) { 
  464. $currency_code = $this->orders_list_currency; 
  465. }elseif( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] == 'wc-reports' && isset( $_COOKIE[ '_wcml_reports_currency' ] ) ) { 
  466. $currency_code = $_COOKIE[ '_wcml_reports_currency' ]; 
  467. }elseif( isset( $_COOKIE[ '_wcml_dashboard_currency' ] ) && is_admin() && !defined( 'DOING_AJAX' ) && $pagenow == 'index.php' ) { 
  468. $currency_code = $_COOKIE[ '_wcml_dashboard_currency' ]; 
  469. }else{ 
  470. $currency_code = $this->multi_currency->get_client_currency(); 
  471.  
  472. return apply_filters( 'wcml_filter_currency_position', $currency_code ); 
  473.  
  474.  
  475. public function get_admin_order_currency_code() { 
  476.  
  477. return $this->check_admin_order_currency_code(); 
  478.  
  479.  
  480. public function save_order_currency_for_filter( $null, $object_id, $meta_key, $single ) { 
  481.  
  482. if( 
  483. $meta_key == '_order_currency' && 
  484. isset( $_GET[ 'post_type' ] ) && 
  485. $_GET[ 'post_type' ] == 'shop_order' && 
  486. !isset( $_GET[ 'post' ] ) && 
  487. get_post_type( $object_id ) == 'shop_order' 
  488. ) { 
  489. remove_filter( 'get_post_metadata', array( $this, 'save_order_currency_for_filter' ), 10, 4); 
  490. $this->orders_list_currency = get_post_meta( $object_id, $meta_key, true ); 
  491. add_filter( 'get_post_metadata', array( $this, 'save_order_currency_for_filter' ), 10, 4); 
  492.  
  493. return $null; 
  494.  
  495. public function filter_currency_thousand_sep_option($value) { 
  496.  
  497. $default_currency = $this->multi_currency->get_default_currency(); 
  498. $currency_code = $this->check_admin_order_currency_code(); 
  499.  
  500. if( $currency_code !== $default_currency && isset( $this->multi_currency->currencies[$currency_code]['thousand_sep'] ) ) { 
  501. $value = $this->multi_currency->currencies[$currency_code]['thousand_sep']; 
  502. return $value; 
  503.  
  504. public function filter_currency_decimal_sep_option($value) { 
  505.  
  506. $default_currency = $this->multi_currency->get_default_currency(); 
  507. $currency_code = $this->check_admin_order_currency_code(); 
  508.  
  509. if( $currency_code !== $default_currency && isset( $this->multi_currency->currencies[$currency_code]['decimal_sep'] ) ) { 
  510. $value = $this->multi_currency->currencies[$currency_code]['decimal_sep']; 
  511.  
  512.  
  513. return $value; 
  514.  
  515. public function filter_currency_num_decimals_option($value) { 
  516. // no other way available (at the moment) to filter currency_num_decimals_option 
  517. $default_currency = $this->multi_currency->get_default_currency(); 
  518.  
  519. $db = debug_backtrace(); 
  520. if( 
  521. isset( $db['8']['function'] ) && isset( $db['5']['function'] ) && 
  522. $db['8']['function'] == 'calculate_shipping_for_package' && $db['5']['function'] == 'add_rate' 
  523. || 
  524. isset( $db['7']['function'] ) && isset( $db['4']['function'] ) && 
  525. $db['7']['function'] == 'calculate_shipping_for_package' && $db['4']['function'] == 'add_rate' 
  526. ) { 
  527. $currency_code = $default_currency; 
  528. }else{ 
  529. $currency_code = $this->check_admin_order_currency_code(); 
  530.  
  531. if( $currency_code !== $default_currency && isset($this->multi_currency->currencies[$currency_code]['num_decimals']) ) { 
  532. $value = $this->multi_currency->currencies[$currency_code]['num_decimals']; 
  533.  
  534. return $value; 
  535.  
  536. public function filter_currency_position_option($value) { 
  537.  
  538. $default_currency = $this->multi_currency->get_default_currency(); 
  539. $currency_code = $this->get_admin_order_currency_code(); 
  540.  
  541. if( $currency_code !== $default_currency && 
  542. isset($this->multi_currency->currencies[$currency_code]['position']) && get_option('woocommerce_currency') != $currency_code && 
  543. in_array($this->multi_currency->currencies[$currency_code]['position'], array('left', 'right', 'left_space', 'right_space'))) { 
  544. $value = $this->multi_currency->currencies[$currency_code]['position']; 
  545. return $value; 
  546.  
  547. public function filter_currency_num_decimals_in_cart( $cart ) { 
  548. $cart->dp = wc_get_price_decimals(); 
  549.  
  550. /** 
  551. * Limitation: If the default currency is configured to display more decimals than the other currencies,  
  552. * the prices in the secondary currencies would be approximated to the number of decimals that they have more. 
  553. */ 
  554. public function price_in_specific_currency( $return, $price, $args ) { 
  555.  
  556. if(isset($args['currency']) && $this->multi_currency->get_client_currency() != $args['currency']) { 
  557. remove_filter( 'wc_price', array( $this, 'price_in_specific_currency' ), 10, 3 ); 
  558. $this->multi_currency->set_client_currency( $args['currency'] ); 
  559. $return = wc_price($price, $args); 
  560. add_filter( 'wc_price', array( $this, 'price_in_specific_currency' ), 10, 3 ); 
  561.  
  562. return $return; 
  563.  
  564.  
  565. public function filter_wc_price_args( $args ) { 
  566.  
  567. if( isset($args['currency']) ) { 
  568.  
  569. if(isset($this->multi_currency->currencies[$args['currency']]['decimal_sep']) ) { 
  570. $args['decimal_separator'] = $this->multi_currency->currencies[$args['currency']]['decimal_sep']; 
  571.  
  572. if(isset($this->multi_currency->currencies[$args['currency']]['thousand_sep']) ) { 
  573. $args['thousand_separator'] = $this->multi_currency->currencies[$args['currency']]['thousand_sep']; 
  574.  
  575. if(isset($this->multi_currency->currencies[$args['currency']]['num_decimals']) ) { 
  576. $args['decimals'] = $this->multi_currency->currencies[$args['currency']]['num_decimals']; 
  577.  
  578. if( isset($this->multi_currency->currencies[$args['currency']]['position']) ) { 
  579. $current_currency = $this->multi_currency->get_client_currency(); 
  580. $this->multi_currency->set_client_currency( $args['currency'] ); 
  581. $args['price_format'] = get_woocommerce_price_format(); 
  582. $this->multi_currency->set_client_currency( $current_currency ); //restore 
  583.  
  584.  
  585. return $args; 
  586.