Advertisement
Guest User

jdkds

a guest
Aug 5th, 2017
1,687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 127.71 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WooCommerce Currency Switcher
  4. Plugin URI: http://publicapi.com.ng
  5. Description: Currency Switcher for WooCommerce - the plugin that allows to the visitors and customers switch currencies on woocommerce store site
  6. Author: GeniusHub NG
  7. Version: 1.1.9
  8. Requires at least: WP 4.1.0
  9. Tested up to: WP 4.7.3
  10. Text Domain: woocommerce-currency-switcher
  11. Domain Path: /languages
  12.  
  13. */
  14.  
  15. if (!defined('ABSPATH')) {
  16. exit; // Exit if accessed directly
  17. }
  18.  
  19. if (defined('DOING_AJAX')) {
  20. if (isset($_REQUEST['action'])) {
  21. //do not recalculate refund amounts when we are in order backend
  22. if ($_REQUEST['action'] == 'woocommerce_refund_line_items') {
  23. return;
  24. }
  25.  
  26. if (isset($_REQUEST['order_id']) AND $_REQUEST['action'] == 'woocommerce_load_order_items') {
  27. return;
  28. }
  29. }
  30. }
  31.  
  32.  
  33. //block for custom support code experiments
  34. /*
  35. if ($_SERVER['REMOTE_ADDR'] != 'xxx.155.xx.190')
  36. {
  37. return;
  38. }
  39. */
  40. //***
  41. define('WOOCS_VERSION', '1.1.9');
  42. define('WOOCS_MIN_WOOCOMMERCE', '2.4');
  43. define('WOOCS_PATH', plugin_dir_path(__FILE__));
  44. define('WOOCS_LINK', plugin_dir_url(__FILE__));
  45. define('WOOCS_PLUGIN_NAME', plugin_basename(__FILE__));
  46.  
  47. //classes
  48. include_once WOOCS_PATH . 'classes/storage.php';
  49. include_once WOOCS_PATH . 'classes/cron.php';
  50. include_once WOOCS_PATH . 'classes/fixed.php';
  51.  
  52. //04-04-2017
  53. final class WOOCS {
  54.  
  55. //http://docs.woothemes.com/wc-apidocs/class-WC_Order.html
  56. public $storage = null;
  57. public $cron = NULL;
  58. public $cron_hook = 'woocs_update_rates_wpcron';
  59. public $wp_cron_period = DAY_IN_SECONDS;
  60. public $settings = array();
  61. public $fixed = NULL;
  62. public $default_currency = 'USD'; //EUR -> set any existed currency here if USD is not exists in your currencies list
  63. public $current_currency = 'USD'; //EUR -> set any existed currency here if USD is not exists in your currencies list
  64. public $currency_positions = array();
  65. public $currency_symbols = array();
  66. public $is_multiple_allowed = true; //from options
  67. public $is_fixed_enabled = false; //from options, works if is_multiple_allowed enabled
  68. public $force_pay_bygeoip_rules = false; //from options, works if is_fixed_enabled enabled
  69. public $is_geoip_manipulation = true; //from options, works if is_multiple_allowed is NOT enabled
  70. public $decimal_sep = '.';
  71. public $thousands_sep = ',';
  72. public $rate_auto_update = ''; //from options
  73. public $shop_is_cached = true;
  74. private $is_first_unique_visit = false;
  75. public $no_cents = array('JPY', 'TWD'); //recount price without cents always!!
  76. public $price_num_decimals = 2;
  77. public $actualized_for = 0; //created especially for woo >= 2.7 as it not possible to use const WOOCOMMERCE_VERSION in the code at some places
  78. public $bones = array(
  79. 'reset_in_multiple' => false, //normal is false
  80. 'disable_currency_switching' => false//normal is false. To force the customer to pay in Welcome currency for example, do it by your own logic
  81. ); //just for some setting for current wp theme adapting - for support only - it is logic hack - be care!!
  82.  
  83. public function __construct() {
  84. $this->storage = new WOOCS_STORAGE(get_option('woocs_storage', 'transient'));
  85. $this->init_no_cents();
  86. if (!defined('DOING_AJAX')) {
  87. //we need it if shop uses cache plugin, in such way prices will be redraw by AJAX
  88. $this->shop_is_cached = get_option('woocs_shop_is_cached', 0);
  89. }
  90.  
  91. //need for woo 2.7
  92. $this->actualized_for = floatval(get_option('woocs_woo_version', 2.6));
  93.  
  94. //+++
  95. add_filter('pre_option_woocommerce_price_num_decimals', array($this, 'woocommerce_price_num_decimals'));
  96. add_filter('woocommerce_add_to_cart_hash', array($this, 'woocommerce_add_to_cart_hash'));
  97. //+++
  98. $currencies = $this->get_currencies();
  99. if (!empty($currencies) AND is_array($currencies)) {
  100. foreach ($currencies as $key => $currency) {
  101. if ($currency['is_etalon']) {
  102. $this->default_currency = $key;
  103. break;
  104. }
  105. }
  106. }
  107.  
  108. //+++
  109. /*
  110. if (!$this->storage->is_isset('woocs_first_unique_visit'))
  111. {
  112. $this->storage->set_val('woocs_first_unique_visit', 0);
  113. }
  114. */
  115. $this->is_geoip_manipulation = get_option('woocs_is_geoip_manipulation', 0);
  116. $this->is_multiple_allowed = get_option('woocs_is_multiple_allowed', 0);
  117. if ($this->is_geoip_manipulation) {
  118. $this->is_multiple_allowed = true;
  119. }
  120. $this->is_fixed_enabled = get_option('woocs_is_fixed_enabled', 0);
  121. $this->force_pay_bygeoip_rules = get_option('woocs_force_pay_bygeoip_rules', 0);
  122. $this->rate_auto_update = get_option('woocs_currencies_rate_auto_update', 'no');
  123. //$this->decimal_sep = wp_specialchars_decode(stripslashes(get_option('woocommerce_price_decimal_sep')), ENT_QUOTES);
  124. //$this->thousands_sep = wp_specialchars_decode(stripslashes(get_option('woocommerce_price_thousand_sep')), ENT_QUOTES);
  125. //+++
  126. $this->currency_positions = array('left', 'right', 'left_space', 'right_space');
  127. $this->init_currency_symbols();
  128. $this->token = get_option('woocs_token');
  129.  
  130. //+++
  131. $is_first_activation = (int) get_option('woocs_first_activation', 0);
  132. if (!$is_first_activation) {
  133. update_option('woocs_first_activation', 1);
  134. update_option('woocs_drop_down_view', 'ddslick');
  135. update_option('woocs_currencies_aggregator', 'yahoo');
  136. update_option('woocs_welcome_currency', $this->default_currency);
  137. update_option('woocs_is_multiple_allowed', 0);
  138. update_option('woocs_is_fixed_enabled', 0);
  139. update_option('woocs_force_pay_bygeoip_rules', 0);
  140. update_option('woocs_is_geoip_manipulation', 0);
  141. update_option('woocs_show_flags', 1);
  142. update_option('woocs_show_money_signs', 1);
  143. update_option('woocs_customer_signs', '');
  144. update_option('woocs_customer_price_format', '');
  145. update_option('woocs_currencies_rate_auto_update', 'no');
  146. update_option('woocs_rate_auto_update_email', 0);
  147. //update_option('woocs_use_curl', 0);
  148. update_option('woocs_storage', 'transient');
  149. update_option('woocs_geo_rules', '');
  150. //update_option('woocs_use_geo_rules', 0);
  151. update_option('woocs_hide_cents', '');
  152. update_option('woocs_decimals', array());
  153. update_option('woocs_price_info', 0);
  154. update_option('woocs_no_cents', '');
  155. update_option('woocs_restrike_on_checkout_page', 0);
  156. update_option('woocs_shop_is_cached', 0);
  157. update_option('woocs_show_approximate_amount', 0);
  158. $this->reset_currency();
  159. //***
  160. update_option('image_default_link_type', 'file'); //http://wordpress.stackexchange.com/questions/9727/link-to-file-url-by-default
  161. }
  162. //+++
  163. //simple checkout itercept
  164. if (isset($_REQUEST['action']) AND $_REQUEST['action'] == 'woocommerce_checkout') {
  165. $_REQUEST['woocommerce-currency-switcher'] = $this->escape($this->storage->get_val('woocs_current_currency'));
  166. $this->current_currency = $this->escape($this->storage->get_val('woocs_current_currency'));
  167. $_REQUEST['woocs_in_order_currency'] = $this->current_currency;
  168. }
  169.  
  170. //paypal query itercept
  171. if (isset($_REQUEST['mc_currency']) AND ! empty($_REQUEST['mc_currency'])) {
  172. if (array_key_exists($_REQUEST['mc_currency'], $currencies)) {
  173. $_REQUEST['woocommerce-currency-switcher'] = $this->escape($_REQUEST['mc_currency']);
  174. }
  175. }
  176.  
  177. //WELCOME USER CURRENCY ACTIVATION
  178. if (intval($this->storage->get_val('woocs_first_unique_visit')) === 0) {
  179. $this->is_first_unique_visit = true;
  180. $this->set_currency($this->get_welcome_currency());
  181. $this->storage->set_val('woocs_first_unique_visit', 1);
  182. }
  183.  
  184. //+++
  185. if (isset($_REQUEST['woocommerce-currency-switcher'])) {
  186. if (array_key_exists($_REQUEST['woocommerce-currency-switcher'], $currencies)) {
  187. $this->storage->set_val('woocs_current_currency', $this->escape($_REQUEST['woocommerce-currency-switcher']));
  188. } else {
  189. $this->storage->set_val('woocs_current_currency', $this->default_currency);
  190. }
  191. }
  192. //+++
  193. //*** check currency in browser address
  194. if (isset($_GET['currency']) AND ! empty($_GET['currency'])) {
  195. $allow_currency_switching = !$this->bones['disable_currency_switching'];
  196.  
  197. //1 issue closing
  198. if (!get_option('woocs_is_multiple_allowed', 0)) {
  199. if (isset($_REQUEST['wc-ajax']) AND ( $_REQUEST['wc-ajax'] == 'get_refreshed_fragments' OR $_REQUEST['wc-ajax'] == 'update_order_review')) {
  200. if (isset($_SERVER['REQUEST_URI'])) {
  201. if (substr_count($_SERVER['REQUEST_URI'], '/checkout/')) {
  202. $allow_currency_switching = false;
  203. $this->reset_currency();
  204. }
  205. }
  206. }
  207. }
  208.  
  209.  
  210.  
  211. if (array_key_exists(strtoupper($_GET['currency']), $currencies) AND $allow_currency_switching) {
  212. $this->storage->set_val('woocs_current_currency', strtoupper($this->escape($_GET['currency'])));
  213. }
  214. }
  215. //+++
  216. if ($this->storage->is_isset('woocs_current_currency')) {
  217. $this->current_currency = $this->storage->get_val('woocs_current_currency');
  218. } else {
  219. $this->current_currency = $this->default_currency;
  220. }
  221. $this->storage->set_val('woocs_default_currency', $this->default_currency);
  222. //+++
  223. //if we want to be paid in the basic currency - not multiple mode
  224. if (isset($_REQUEST['action']) AND ! get_option('woocs_is_multiple_allowed', 0)) {
  225. //old code for woocomerce < 2.4, left for comatibility with old versions of woocommerce
  226. if ($_REQUEST['action'] == 'woocommerce_update_order_review') {
  227. $this->reset_currency();
  228. }
  229. }
  230.  
  231. //+++ FILTERS
  232. add_filter('woocommerce_paypal_args', array($this, 'apply_conversion'));
  233. add_filter('woocommerce_paypal_supported_currencies', array($this, 'enable_custom_currency'), 9999);
  234. add_filter('woocommerce_currency_symbol', array($this, 'woocommerce_currency_symbol'), 9999);
  235. add_filter('woocommerce_currency', array($this, 'get_woocommerce_currency'), 9999);
  236. add_filter('wc_get_template', array($this, 'wc_get_template'), 9999, 5); //from woo 2.7 its nessesary for new order email
  237. //main recount hook
  238. if ($this->is_multiple_allowed) {
  239. if (version_compare($this->actualized_for, 2.7, '>=')) {
  240. //woo >= v.2.7
  241. add_filter('woocommerce_product_get_price', array($this, 'raw_woocommerce_price'), 9999, 2);
  242. //wp-content\plugins\woocommerce\includes\abstracts\abstract-wc-data.php
  243. //protected function get_prop
  244. add_filter('woocommerce_product_variation_get_price', array($this, 'raw_woocommerce_price'), 9999, 2);
  245. add_filter('woocommerce_product_variation_get_regular_price', array($this, 'raw_woocommerce_price'), 9999, 2);
  246. add_filter('woocommerce_product_variation_get_sale_price', array($this, 'raw_woocommerce_price'), 9999, 2);
  247.  
  248. //CART FIX FOR VARIABLE PRODUCTS - removed, resolved by another way
  249. //add_filter('woocommerce_cart_product_subtotal', array($this, 'woocommerce_cart_product_subtotal'), 9999, 4);
  250. //add_filter('woocommerce_cart_product_price', array($this, 'woocommerce_cart_product_price'), 9999, 2);
  251. //add_filter('woocommerce_cart_subtotal', array($this, 'woocommerce_cart_subtotal2'), 9999, 3);
  252. //add_filter('woocommerce_cart_contents_total', array($this, 'woocommerce_cart_contents_total2'), 9999, 1);
  253. } else {
  254. add_filter('woocommerce_get_price', array($this, 'raw_woocommerce_price'), 9999, 2);
  255. }
  256. } else {
  257. add_filter('raw_woocommerce_price', array($this, 'raw_woocommerce_price'), 9999);
  258. }
  259.  
  260.  
  261. //fix for single page with variables products
  262. if (version_compare($this->actualized_for, 2.7, '>=') AND $this->is_multiple_allowed) {
  263. //woo >= v.2.7
  264. //add_filter('woocommerce_available_variation', array($this, 'woocommerce_available_variation'), 9999, 3);
  265. }
  266.  
  267.  
  268. //+++
  269. if ($this->is_multiple_allowed) {
  270. //wp-content\plugins\woocommerce\includes\abstracts\abstract-wc-product.php #795
  271. /* Alda: Had to removed the filter as it is redundant with the woocommerce_get_price hook */
  272. //I back it 07-01-2016 because of it is really need.
  273. //Comment next 2 hooks if double recount is for sale price http://c2n.me/3sCQFkX
  274.  
  275.  
  276. if (version_compare($this->actualized_for, 2.7, '>=')) {
  277. //woo >= v.2.7
  278. add_filter('woocommerce_product_get_regular_price', array($this, 'raw_woocommerce_price'), 9999, 2);
  279. } else {
  280. add_filter('woocommerce_get_regular_price', array($this, 'raw_woocommerce_price'), 9999, 2);
  281. }
  282.  
  283.  
  284. if (version_compare($this->actualized_for, 2.7, '>=')) {
  285. //woo >= v.2.7
  286. add_filter('woocommerce_product_get_sale_price', array($this, 'raw_woocommerce_price_sale'), 9999, 2);
  287. } else {
  288. add_filter('woocommerce_get_sale_price', array($this, 'raw_woocommerce_price'), 9999, 2);
  289. }
  290.  
  291. //***
  292.  
  293. add_filter('woocommerce_get_variation_regular_price', array($this, 'raw_woocommerce_price'), 9999, 4);
  294. add_filter('woocommerce_get_variation_sale_price', array($this, 'raw_woocommerce_price'), 9999, 4);
  295. add_filter('woocommerce_variation_prices', array($this, 'woocommerce_variation_prices'), 9999, 3);
  296. //***
  297. //add_filter('woocommerce_get_variation_price', array($this, 'raw_woocommerce_price'), 9999, 1);
  298. add_filter('woocommerce_variation_prices_price', array($this, 'woocommerce_variation_prices'), 9999, 3);
  299. add_filter('woocommerce_variation_prices_regular_price', array($this, 'woocommerce_variation_prices'), 9999, 3);
  300. add_filter('woocommerce_variation_prices_sale_price', array($this, 'woocommerce_variation_prices'), 9999, 3);
  301. add_filter('woocommerce_get_variation_prices_hash', array($this, 'woocommerce_get_variation_prices_hash'), 9999, 3);
  302. }
  303. //***
  304.  
  305.  
  306. add_filter('woocommerce_price_format', array($this, 'woocommerce_price_format'), 9999);
  307. add_filter('woocommerce_thankyou_order_id', array($this, 'woocommerce_thankyou_order_id'), 9999);
  308. add_filter('woocommerce_before_resend_order_emails', array($this, 'woocommerce_before_resend_order_emails'), 1);
  309. add_filter('woocommerce_email_actions', array($this, 'woocommerce_email_actions'), 10);
  310. add_action('woocommerce_order_status_completed', array($this, 'woocommerce_order_status_completed'), 1);
  311. add_action('woocommerce_order_status_completed_notification', array($this, 'woocommerce_order_status_completed_notification'), 1);
  312. //add_filter('formatted_woocommerce_price', array($this, 'formatted_woocommerce_price'), 9999);
  313. add_filter('woocommerce_package_rates', array($this, 'woocommerce_package_rates'), 9999);
  314.  
  315. //sometimes woocommerce_product_is_on_sale is works on single page for show OnSale icon for all currencies
  316. //add_filter('woocommerce_product_is_on_sale', array($this, 'woocommerce_product_is_on_sale'), 9999, 2);
  317. //for shop cart
  318. add_filter('woocommerce_cart_totals_order_total_html', array($this, 'woocommerce_cart_totals_order_total_html'), 9999, 1);
  319. add_filter('wc_price_args', array($this, 'wc_price_args'), 9999);
  320.  
  321.  
  322. //for refreshing mini-cart widget
  323. add_filter('woocommerce_before_mini_cart', array($this, 'woocommerce_before_mini_cart'), 9999);
  324. add_filter('woocommerce_after_mini_cart', array($this, 'woocommerce_after_mini_cart'), 9999);
  325.  
  326.  
  327. //shipping
  328. add_filter('woocommerce_shipping_free_shipping_is_available', array($this, 'woocommerce_shipping_free_shipping_is_available'), 99, 2);
  329. //for woo >= 2.6.x
  330. add_filter('woocommerce_shipping_legacy_free_shipping_is_available', array($this, 'woocommerce_shipping_free_shipping_is_available'), 99, 2);
  331.  
  332.  
  333. //add_filter('was_calculate_shipping_costs', array($this, 'was_calculate_shipping_costs'), 99, 4);
  334. //add_filter('woocommerce_update_shipping_method', array($this, 'woocommerce_update_shipping_method'), 1);
  335. //orders view on front
  336. //add_filter('woocommerce_view_order', array($this, 'woocommerce_view_order'), 1);
  337. add_action('woocommerce_get_order_currency', array($this, 'woocommerce_get_order_currency'), 1, 2);
  338. //add_filter('woocommerce_get_formatted_order_total', array($this, 'woocommerce_get_formatted_order_total'), 1, 2);
  339. //+++
  340. //+++ AJAX ACTIONS
  341. add_action('wp_ajax_woocs_save_etalon', array($this, 'save_etalon'));
  342. add_action('wp_ajax_woocs_get_rate', array($this, 'get_rate'));
  343.  
  344. add_action('wp_ajax_woocs_convert_currency', array($this, 'woocs_convert_currency'));
  345. add_action('wp_ajax_nopriv_woocs_convert_currency', array($this, 'woocs_convert_currency'));
  346.  
  347. add_action('wp_ajax_woocs_rates_current_currency', array($this, 'woocs_rates_current_currency'));
  348. add_action('wp_ajax_nopriv_woocs_rates_current_currency', array($this, 'woocs_rates_current_currency'));
  349.  
  350. add_action('wp_ajax_woocs_get_products_price_html', array($this, 'woocs_get_products_price_html'));
  351. add_action('wp_ajax_nopriv_woocs_get_products_price_html', array($this, 'woocs_get_products_price_html'));
  352.  
  353. add_action('wp_ajax_woocs_recalculate_order_data', array($this, 'woocs_recalculate_order_data'));
  354. //+++
  355.  
  356. add_action('woocommerce_settings_tabs_array', array($this, 'woocommerce_settings_tabs_array'), 9999);
  357. add_action('woocommerce_settings_tabs_woocs', array($this, 'print_plugin_options'), 9999);
  358.  
  359. //+++
  360. add_action('widgets_init', array($this, 'widgets_init'));
  361. add_action('wp_head', array($this, 'wp_head'), 999);
  362. add_action('wp_footer', array($this, 'wp_footer'), 9999);
  363. add_action('body_class', array($this, 'body_class'), 9999);
  364. //***
  365. add_action('save_post', array($this, 'save_post'), 1);
  366. add_action('admin_head', array($this, 'admin_head'), 1);
  367. //add_action('admin_footer-edit.php', array($this, 'admin_footer'), 99);//UNDER DEV
  368. //add_filter('bulk_actions-edit-shop_order', array($this, 'shop_order_bulk_actions'));//UNDER DEV
  369. add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
  370. add_action('admin_init', array($this, 'admin_init'), 1);
  371. //price formatting on front ***********
  372. if (version_compare($this->actualized_for, 2.7, '>=')) {
  373. //woo >= v.2.7
  374. add_action('woocommerce_get_price_html', array($this, 'woocommerce_price_html'), 1, 2);
  375. } else {
  376. add_action('woocommerce_price_html', array($this, 'woocommerce_price_html'), 1, 2);
  377. }
  378.  
  379. //if ($this->is_multiple_allowed)
  380. {
  381. if (version_compare($this->actualized_for, 2.7, '<')) {
  382. add_action('woocommerce_variable_price_html', array($this, 'woocommerce_price_html'), 1, 2);
  383. }
  384.  
  385. add_action('woocommerce_variable_sale_price_html', array($this, 'woocommerce_price_html'), 1, 2);
  386. add_action('woocommerce_sale_price_html', array($this, 'woocommerce_price_html'), 1, 2);
  387. add_action('woocommerce_grouped_price_html', array($this, 'woocommerce_price_html'), 1, 2);
  388. }
  389.  
  390.  
  391.  
  392. //*** additional
  393. //wpo_wcpdf_order_number is -> compatibility for https://wordpress.org/plugins/woocommerce-pdf-invoices-packing-slips/stats/
  394. //add_action('wpo_wcpdf_order_number', array($this, 'wpo_wcpdf_order_number'), 1);
  395. add_action('wpo_wcpdf_process_template_order', array($this, 'wpo_wcpdf_process_template_order'), 1, 2);
  396. add_action('woocs_exchange_value', array($this, 'woocs_exchange_value'), 1);
  397. //add_action('wcml_raw_price_amount', array($this, 'wcml_raw_price_amount'), 1);
  398. //for coupons
  399. add_filter('woocommerce_coupon_get_discount_amount', array($this, 'woocommerce_coupon_get_discount_amount'), 9999, 5);
  400. add_filter('woocommerce_coupon_validate_minimum_amount', array($this, 'woocommerce_coupon_validate_minimum_amount'), 9999, 2);
  401. add_filter('woocommerce_coupon_validate_maximum_amount', array($this, 'woocommerce_coupon_validate_maximum_amount'), 9999, 2);
  402. add_filter('woocommerce_coupon_error', array($this, 'woocommerce_coupon_error'), 9999, 3);
  403.  
  404.  
  405. //***
  406. add_filter('woocommerce_checkout_update_order_review', array($this, 'woocommerce_checkout_update_order_review'), 9999);
  407.  
  408.  
  409.  
  410. //*** comment it!!! It is custom work experiments ....
  411. //add_action('woocommerce_cart_get_taxes', array($this, 'woocommerce_cart_get_taxes'), 11, 2);
  412. //add_filter('woocommerce_cart_total', array($this, 'woocommerce_cart_total'), 11, 1);
  413. //add_filter('woocommerce_after_calculate_totals', array($this, 'woocommerce_after_calculate_totals'), 9999, 1);
  414. //***
  415. //*************************************
  416. add_shortcode('woocs', array($this, 'woocs_shortcode'));
  417. add_shortcode('woocs_get_sign_rate', array($this, 'get_sign_rate'));
  418. add_shortcode('woocs_converter', array($this, 'woocs_converter'));
  419. add_shortcode('woocs_rates', array($this, 'woocs_rates'));
  420. add_shortcode('woocs_show_current_currency', array($this, 'woocs_show_current_currency'));
  421. add_shortcode('woocs_show_custom_price', array($this, 'woocs_show_custom_price'));
  422. add_shortcode('woocs_geo_hello', array($this, 'woocs_geo_hello'));
  423.  
  424. if (get_option('woocs_is_multiple_allowed', 0)) {
  425. add_action('the_post', array($this, 'the_post'), 1);
  426. add_action('load-post.php', array($this, 'admin_action_post'), 1);
  427. }
  428.  
  429. //+++
  430. add_action('woocs_update_rates_wpcron', array($this, 'rate_auto_update'), 10);
  431. $this->cron = new PN_WP_CRON_WOOCS('woocs_rates_wpcron');
  432. $this->wp_cron_period = (int) $this->get_woocs_cron_schedules($this->rate_auto_update);
  433. $this->make_rates_auto_update();
  434. //***
  435. if ($this->is_fixed_enabled OR $this->is_geoip_manipulation) {
  436. $this->fixed = new WOOCS_FIXED();
  437. }
  438. }
  439.  
  440. //for normal shippng update if to change currency
  441. public function woocommerce_add_to_cart_hash($hash) {
  442. return "";
  443. }
  444.  
  445. public function init() {
  446. if (!class_exists('WooCommerce')) {
  447. return;
  448. }
  449.  
  450. //global $wp;
  451. wp_enqueue_script('jquery');
  452.  
  453. //+++
  454. load_plugin_textdomain('woocommerce-currency-switcher', false, dirname(plugin_basename(__FILE__)) . '/languages');
  455.  
  456.  
  457. //filters
  458. add_filter('plugin_action_links_' . WOOCS_PLUGIN_NAME, array($this, 'plugin_action_links'));
  459. add_filter('woocommerce_currency_symbol', array($this, 'woocommerce_currency_symbol'), 9999);
  460.  
  461. //***
  462. //if we use GeoLocation
  463. //$this->init_geo_currency();
  464. //set default cyrrency for wp-admin of the site
  465. if (is_admin() AND ! is_ajax()) {
  466. $this->current_currency = $this->default_currency;
  467. } else {
  468. //if we are in the a product backend and loading its variations
  469. if (is_ajax() AND ( isset($_REQUEST['action']) AND $_REQUEST['action'] == 'woocommerce_load_variations')) {
  470. $this->current_currency = $this->default_currency;
  471. }
  472. }
  473.  
  474. if (is_ajax()) {
  475. $actions = false;
  476. //code for manual order adding
  477. if (isset($_REQUEST['action']) AND $_REQUEST['action'] == 'woocommerce_add_order_item') {
  478. $actions = true;
  479. }
  480.  
  481. if (isset($_REQUEST['action']) AND $_REQUEST['action'] == 'woocommerce_save_order_items') {
  482. $actions = true;
  483. }
  484.  
  485. if (isset($_REQUEST['action']) AND $_REQUEST['action'] == 'woocommerce_calc_line_taxes') {
  486. $actions = true;
  487. }
  488. //***
  489. if ($actions AND current_user_can('edit_shop_orders')) {
  490. $this->current_currency = $this->default_currency;
  491. //check if is order has installed currency
  492. $currency = get_post_meta($_REQUEST['order_id'], '_order_currency', TRUE);
  493. if (!empty($currency)) {
  494. $this->current_currency = $currency;
  495. }
  496. }
  497. }
  498.  
  499. if (is_ajax() AND isset($_REQUEST['action'])
  500. AND $_REQUEST['action'] == 'woocommerce_mark_order_status'
  501. AND isset($_REQUEST['status']) AND $_REQUEST['status'] == 'completed'
  502. AND isset($_REQUEST['order_id'])
  503. ) {
  504. $currency = get_post_meta($_REQUEST['order_id'], '_order_currency', true);
  505. if (!empty($currency)) {
  506. $_REQUEST['woocs_in_order_currency'] = $currency;
  507. $this->current_currency = $currency;
  508. }
  509. }
  510.  
  511.  
  512.  
  513. //if we want to be paid in the basic currency - not multiple mode
  514. if (!get_option('woocs_is_multiple_allowed', 0)) {
  515.  
  516. /*
  517. * moved to public function woocommerce_checkout_update_order_review()
  518. if (isset($_GET['wc-ajax']) AND $_GET['wc-ajax'] == 'update_order_review')
  519. {
  520. $this->reset_currency();
  521. }
  522. */
  523.  
  524. //compatibility for WC_Gateway_PayPal_Express_AngellEYE
  525. if (isset($_GET['wc-api']) AND isset($_GET['pp_action']) AND isset($_GET['use_paypal_credit'])) {
  526. if ($_GET['pp_action'] == 'expresscheckout') {
  527. $this->reset_currency();
  528. }
  529. }
  530. }
  531.  
  532.  
  533. if ($this->force_pay_bygeoip_rules) {
  534. if ((is_checkout() OR is_checkout_pay_page()) AND ! isset($_GET['key'])) {
  535. $this->force_pay_bygeoip_rules();
  536. }
  537.  
  538. /*
  539. * moved to public function woocommerce_checkout_update_order_review()
  540. if (isset($_REQUEST['wc-ajax']) AND ( $_REQUEST['wc-ajax'] == 'get_refreshed_fragments' OR $_REQUEST['wc-ajax'] == 'update_order_review'))
  541. {
  542. if (isset($_SERVER['REQUEST_URI']))
  543. {
  544. if (substr_count($_SERVER['REQUEST_URI'], '/checkout/'))
  545. {
  546. $this->force_pay_bygeoip_rules();
  547. }
  548. }
  549. }
  550. */
  551. }
  552.  
  553. //***
  554. //Show Approx. data info
  555. if ($this->is_use_geo_rules() AND get_option('woocs_show_approximate_amount', 0) AND ( isset(WC()->cart)/* AND WC()->cart->subtotal > 0 */)) {
  556.  
  557. add_filter('woocommerce_cart_total', array($this, 'woocommerce_cart_total'), 9999, 1);
  558.  
  559. add_filter('woocommerce_cart_item_price', array($this, 'woocommerce_cart_item_price'), 9999, 3);
  560. add_filter('woocommerce_cart_item_subtotal', array($this, 'woocommerce_cart_item_subtotal'), 9999, 3);
  561. add_filter('woocommerce_cart_subtotal', array($this, 'woocommerce_cart_subtotal'), 9999, 3);
  562.  
  563. add_filter('woocommerce_cart_totals_taxes_total_html', array($this, 'woocommerce_cart_totals_taxes_total_html'), 9999, 1);
  564. add_filter('woocommerce_cart_tax_totals', array($this, 'woocommerce_cart_tax_totals'), 9999, 2);
  565. add_filter('woocommerce_cart_shipping_method_full_label', array($this, 'woocommerce_cart_shipping_method_full_label'), 9999, 2);
  566. }
  567.  
  568. //woo version control for enabling right functionality after migration from woo 2.6.x to 3.x.x
  569. if ($this->actualized_for !== floatval(WOOCOMMERCE_VERSION)) {
  570. update_option('woocs_woo_version', WOOCOMMERCE_VERSION);
  571. }
  572.  
  573. //***
  574. }
  575.  
  576. public function make_rates_auto_update($reset = false) {
  577. if ($this->rate_auto_update != 'no' AND ! empty($this->rate_auto_update)) {
  578. if ($this->wp_cron_period) {
  579. if ($reset) {
  580. $this->cron->reset($this->cron_hook, $this->wp_cron_period);
  581. }
  582.  
  583. $this->woocs_wpcron_init();
  584. }
  585. }
  586. }
  587.  
  588. public function woocs_wpcron_init($remove = false) {
  589. if ($remove) {
  590. $this->cron->remove($this->cron_hook);
  591. return;
  592. }
  593.  
  594. if ($this->wp_cron_period) {
  595. if (!$this->cron->is_attached($this->cron_hook, $this->wp_cron_period)) {
  596. $this->cron->attach($this->cron_hook, time(), $this->wp_cron_period);
  597. }
  598.  
  599. $this->cron->process();
  600. }
  601. }
  602.  
  603. public function get_woocs_cron_schedules($key = '') {
  604. $schedules = array(
  605. 'min15' => 15 * MINUTE_IN_SECONDS,
  606. 'min30' => 30 * MINUTE_IN_SECONDS,
  607. 'min45' => 45 * MINUTE_IN_SECONDS,
  608. 'hourly' => HOUR_IN_SECONDS,
  609. 'twicedaily' => HOUR_IN_SECONDS * 12,
  610. 'daily' => DAY_IN_SECONDS,
  611. 'week' => WEEK_IN_SECONDS,
  612. 'month' => WEEK_IN_SECONDS * 4,
  613. 'min1' => MINUTE_IN_SECONDS,
  614. );
  615. //print_r($schedules);
  616. if (!empty($key) AND isset($schedules[$key])) {
  617. return (int) $schedules[$key];
  618. } else {
  619. return NULL;
  620. }
  621.  
  622. return $schedules;
  623. }
  624.  
  625. public function get_currency_price_num_decimals($currency, $val = 2) {
  626. $currencies = $this->get_currencies();
  627. if (isset($currencies[$currency]['decimals'])) {
  628. $val = $currencies[$currency]['decimals'];
  629. }
  630.  
  631. return intval($val);
  632. }
  633.  
  634. public function woocommerce_price_num_decimals($default) {
  635. $this->price_num_decimals = $this->get_currency_price_num_decimals($this->current_currency);
  636.  
  637. return $this->price_num_decimals;
  638. }
  639.  
  640. public function body_class($classes) {
  641. $classes[] = 'currency-' . strtolower($this->current_currency);
  642. return $classes;
  643. }
  644.  
  645. public function init_currency_symbols() {
  646. //includes/wc-core-functions.php #217
  647. $this->currency_symbols = array(
  648. '&#36;', '&euro;', '&yen;', '&#1088;&#1091;&#1073;.', '&#1075;&#1088;&#1085;.', '&#8361;',
  649. '&#84;&#76;', 'د.إ', '&#2547;', '&#82;&#36;', '&#1083;&#1074;.',
  650. '&#107;&#114;', '&#82;', '&#75;&#269;', '&#82;&#77;', 'kr.', '&#70;&#116;',
  651. 'Rp', 'Rs', '&#8377;', 'Kr.', '&#8362;', '&#8369;', '&#122;&#322;', '&#107;&#114;',
  652. '&#67;&#72;&#70;', '&#78;&#84;&#36;', '&#3647;', '&pound;', 'lei', '&#8363;',
  653. '&#8358;', 'Kn', '-----'
  654. );
  655.  
  656. $this->currency_symbols = apply_filters('woocs_currency_symbols', array_merge($this->currency_symbols, $this->get_customer_signs()));
  657. }
  658.  
  659. private function init_no_cents() {
  660. $no_cents = get_option('woocs_no_cents', '');
  661. $currencies = $this->get_currencies();
  662. //***
  663. if (!empty($currencies) AND is_array($currencies)) {
  664. $currencies = array_keys($currencies);
  665. $currencies = array_map('strtolower', $currencies);
  666. if (!empty($no_cents)) {
  667. $no_cents = explode(',', $no_cents);
  668. if (!empty($no_cents) AND is_array($no_cents)) {
  669. foreach ($no_cents as $value) {
  670. if (in_array(strtolower($value), $currencies)) {
  671. $this->no_cents[] = $value;
  672. }
  673. }
  674. }
  675. }
  676. }
  677.  
  678. return $this->no_cents;
  679. }
  680.  
  681. //for auto rate update sheduler
  682. public function rate_auto_update() {
  683. $currencies = $this->get_currencies();
  684. //***
  685. $_REQUEST['no_ajax'] = TRUE;
  686. $request = array();
  687. foreach ($currencies as $key => $currency) {
  688. if ($currency['is_etalon'] == 1) {
  689. continue;
  690. }
  691. $_REQUEST['currency_name'] = $currency['name'];
  692. $request[$key] = (float) $this->get_rate();
  693. }
  694. //*** checking and assigning data
  695. foreach ($currencies as $key => $currency) {
  696. if ($currency['is_etalon'] == 1) {
  697. continue;
  698. }
  699. if (isset($request[$key]) AND ! empty($request[$key]) AND $request[$key] > 0) {
  700. $currencies[$key]['rate'] = $request[$key];
  701. }
  702. }
  703.  
  704. //***
  705. static $email_is_sent = false;
  706. if (isset($_REQUEST['woocs_cron_running']) AND ! $email_is_sent) {
  707. if (get_option('woocs_rate_auto_update_email', 0)) {
  708. $message = sprintf(__('<strong>Base currency of the site is: %s</strong>', 'woocommerce-currency-switcher'), $this->default_currency);
  709. $message .= '<br /><br /><ul>';
  710. foreach ($currencies as $code => $curr) {
  711. if ($code == $this->default_currency) {
  712. continue;
  713. }
  714.  
  715. $message .= '<li><b>' . $code . '</b>: <i>' . $curr['rate'] . '</i><br /><br /></li>';
  716. }
  717. $message .= '</ul>';
  718. //***
  719. $headers = 'MIME-Version: 1.0' . "\r\n";
  720. $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
  721. mail(get_bloginfo('admin_email'), 'Currency rates updated on ' . get_bloginfo('name'), $message, $headers);
  722. }
  723.  
  724. $email_is_sent = true;
  725. }
  726. //***
  727.  
  728. update_option('woocs', $currencies);
  729. }
  730.  
  731. public function init_geo_currency() {
  732. $done = false;
  733. $pd = WC_Geolocation::geolocate_ip();
  734. $this->storage->set_val('woocs_user_country', $pd['country']);
  735. //***
  736. if ($this->is_use_geo_rules()) {
  737. $rules = $this->get_geo_rules();
  738.  
  739. $is_allowed = $this->is_first_unique_visit AND function_exists('wc_clean') AND function_exists('wp_validate_redirect');
  740. if ($is_allowed) {
  741.  
  742. if (isset($pd['country']) AND ! empty($pd['country'])) {
  743.  
  744. if (!empty($rules)) {
  745. foreach ($rules as $curr => $countries) {
  746. if (!empty($countries) AND is_array($countries)) {
  747. foreach ($countries as $country) {
  748. if ($country == $pd['country']) {
  749. $this->set_currency($curr);
  750. $done = true;
  751. break(2);
  752. }
  753. }
  754. }
  755. }
  756. }
  757. }
  758. }
  759. }
  760.  
  761. //$this->storage->set_val('woocs_first_unique_visit', 1);
  762.  
  763. return $done;
  764. }
  765.  
  766. public function get_currency_by_country($country_code) {
  767. $rules = $this->get_geo_rules();
  768. if (!empty($rules)) {
  769. foreach ($rules as $currency => $countries) {
  770. if (!empty($countries) AND is_array($countries)) {
  771. foreach ($countries as $country) {
  772. if ($country == $country_code) {
  773. return $currency;
  774. }
  775. }
  776. }
  777. }
  778. }
  779.  
  780. return '';
  781. }
  782.  
  783. /**
  784. * Show action links on the plugin screen
  785. */
  786. public function plugin_action_links($links) {
  787. return array_merge(array(
  788. '<a href="' . admin_url('admin.php?page=wc-settings&tab=woocs') . '">' . __('Settings', 'woocommerce-currency-switcher') . '</a>',
  789. '<a target="_blank" href="' . esc_url('http://publicapi.com.ng/') . '">' . __('Documentation', 'woocommerce-currency-switcher') . '</a>'
  790. ), $links);
  791.  
  792. return $links;
  793. }
  794.  
  795. public function widgets_init() {
  796. require_once WOOCS_PATH . 'classes/widgets/widget-woocs-selector.php';
  797. require_once WOOCS_PATH . 'classes/widgets/widget-currency-rates.php';
  798. require_once WOOCS_PATH . 'classes/widgets/widget-currency-converter.php';
  799. register_widget('WOOCS_SELECTOR');
  800. register_widget('WOOCS_RATES');
  801. register_widget('WOOCS_CONVERTER');
  802. //overides woocs slider js
  803. wp_register_script('wc-price-slider', WOOCS_LINK . 'js/price-slider.js', array('jquery', 'jquery-ui-slider', 'wc-jquery-ui-touchpunch'));
  804. }
  805.  
  806. public function admin_enqueue_scripts() {
  807. if (isset($_GET['tab']) AND $_GET['tab'] == 'woocs') {
  808. wp_enqueue_style('woocommerce-currency-switcher-options', WOOCS_LINK . 'css/options.css');
  809. }
  810. }
  811.  
  812. public function admin_head() {
  813. if (isset($_GET['woocs_reset'])) {
  814. delete_option('woocs');
  815. }
  816. //wp_enqueue_scripts('jquery');
  817. if (isset($_GET['page']) AND isset($_GET['tab'])) {
  818. if ($_GET['page'] == 'wc-settings'/* AND $_GET['tab'] == 'woocs' */) {
  819. wp_enqueue_script('woocs-admin', WOOCS_LINK . 'js/admin.js', array('jquery'));
  820. }
  821. }
  822. }
  823.  
  824. public function admin_footer() {
  825. global $post_type;
  826.  
  827. if ('shop_order' == $post_type) {
  828. ?>
  829. <script type="text/javascript">
  830. jQuery(function () {
  831. jQuery('<option>').val('woocs_convert_to_default').text('<?php _e('WOOCS - recalculate orders', 'woocommerce-currency-switcher') ?>').appendTo('select[name="action"]');
  832. jQuery('<option>').val('woocs_convert_to_default').text('<?php _e('WOOCS - recalculate orders', 'woocommerce-currency-switcher') ?>').appendTo('select[name="action2"]');
  833. });
  834. </script>
  835. <?php
  836. }
  837. }
  838.  
  839. /**
  840. * Process the new bulk actions for changing order currency to default.
  841. */
  842. public function shop_order_bulk_actions() {
  843. $wp_list_table = _get_list_table('WP_Posts_List_Table');
  844. $action = $wp_list_table->current_action();
  845.  
  846. //print_r($action);exit;
  847. }
  848.  
  849. public function admin_init() {
  850. if (get_option('woocs_is_multiple_allowed', 0)) {
  851. add_meta_box('woocs_order_metabox', __('WOOCS Order Info', 'woocommerce-currency-switcher'), array($this, 'woocs_order_metabox'), 'shop_order', 'side', 'default');
  852. }
  853. }
  854.  
  855. //for orders hook
  856. public function save_post($order_id) {
  857. if (current_user_can('edit_shop_orders')) {
  858. if (!empty($_POST)) {
  859. global $post;
  860. if (is_object($post)) {
  861. if (($post->post_type == 'shop_order' || $post->post_type == 'shop_subscription') AND isset($_POST['woocs_order_currency'])) {
  862. $currencies = $this->get_currencies();
  863. $currencies_keys = array_keys($currencies);
  864. $currency = $this->escape($_POST['woocs_order_currency']);
  865. if (in_array($currency, $currencies_keys)) {
  866.  
  867. //changing order currency
  868. update_post_meta($order_id, '_order_currency', $currency);
  869.  
  870. update_post_meta($order_id, '_woocs_order_rate', $currencies[$currency]['rate']);
  871. wc_add_order_item_meta($order_id, '_woocs_order_rate', $currencies[$currency]['rate'], true);
  872.  
  873. update_post_meta($order_id, '_woocs_order_base_currency', $this->default_currency);
  874. wc_add_order_item_meta($order_id, '_woocs_order_base_currency', $this->default_currency, true);
  875.  
  876. update_post_meta($order_id, '_woocs_order_currency_changed_mannualy', time());
  877. wc_add_order_item_meta($order_id, '_woocs_order_currency_changed_mannualy', time(), true);
  878. }
  879. }
  880. }
  881. }
  882. }
  883. }
  884.  
  885. //for orders hook
  886. public function the_post($post) {
  887. if (is_object($post) AND $post->post_type == 'shop_order') {
  888. $currency = get_post_meta($post->ID, '_order_currency', true);
  889. if (!empty($currency)) {
  890. $_REQUEST['woocs_in_order_currency'] = $currency;
  891. $this->current_currency = $currency;
  892. }
  893. }
  894.  
  895. return $post;
  896. }
  897.  
  898. //for orders hook
  899. public function admin_action_post() {
  900. if (isset($_GET['post'])) {
  901. $post_id = $_GET['post'];
  902. $post = get_post($post_id);
  903. if (is_object($post) AND $post->post_type == 'shop_order') {
  904. $currency = get_post_meta($post->ID, '_order_currency', true);
  905. if (!empty($currency)) {
  906. $_REQUEST['woocs_in_order_currency'] = $currency;
  907. $this->current_currency = $currency;
  908. }
  909. }
  910. }
  911. }
  912.  
  913. public function woocs_order_metabox($post) {
  914. $data = array();
  915. $data['post'] = $post;
  916. $data['order'] = new WC_Order($post->ID);
  917. echo $this->render_html(WOOCS_PATH . 'views/woocs_order_metabox.php', $data);
  918. }
  919.  
  920. public function wp_head() {
  921. //*** if the site is visited for the first time lets execute geo ip conditions
  922. $this->init_geo_currency();
  923. //***
  924. wp_enqueue_script('jquery');
  925. wp_enqueue_script('wc-price-slider');
  926. $currencies = $this->get_currencies();
  927. ?>
  928. <script type="text/javascript">
  929. var woocs_is_mobile = <?php echo (int) wp_is_mobile() ?>;
  930. var woocs_drop_down_view = "<?php echo $this->get_drop_down_view(); ?>";
  931. var woocs_current_currency = <?php echo json_encode((isset($currencies[$this->current_currency]) ? $currencies[$this->current_currency] : $currencies[$this->default_currency])) ?>;
  932. var woocs_default_currency = <?php echo json_encode($currencies[$this->default_currency]) ?>;
  933. var woocs_array_of_get = '{}';
  934. <?php if (!empty($_GET)): ?>
  935. <?php
  936. //sanitization of $_GET array
  937. $sanitized_get_array = array();
  938. foreach ($_GET as $key => $value) {
  939. $sanitized_get_array[$this->escape($key)] = $this->escape($value);
  940. }
  941. ?>
  942. woocs_array_of_get = '<?php echo str_replace("'", "", json_encode($sanitized_get_array)); ?>';
  943. <?php endif; ?>
  944.  
  945. woocs_array_no_cents = '<?php echo json_encode($this->no_cents); ?>';
  946.  
  947. var woocs_ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
  948. var woocs_lang_loading = "<?php _e('loading', 'woocommerce-currency-switcher') ?>";
  949. var woocs_shop_is_cached =<?php echo (int) $this->shop_is_cached ?>;
  950. </script>
  951. <?php
  952. if ($this->get_drop_down_view() == 'ddslick') {
  953. wp_enqueue_script('jquery.ddslick.min', WOOCS_LINK . 'js/jquery.ddslick.min.js', array('jquery'));
  954. }
  955.  
  956. if ($this->get_drop_down_view() == 'chosen' OR $this->get_drop_down_view() == 'chosen_dark') {
  957. wp_enqueue_script('chosen-drop-down', WOOCS_LINK . 'js/chosen/chosen.jquery.min.js', array('jquery'));
  958. wp_enqueue_style('chosen-drop-down', WOOCS_LINK . 'js/chosen/chosen.min.css');
  959. //dark chosen
  960. if ($this->get_drop_down_view() == 'chosen_dark') {
  961. wp_enqueue_style('chosen-drop-down-dark', WOOCS_LINK . 'js/chosen/chosen-dark.css');
  962. }
  963. }
  964.  
  965. if ($this->get_drop_down_view() == 'wselect') {
  966. wp_enqueue_script('woocs_wselect', WOOCS_LINK . 'js/wselect/wSelect.min.js', array('jquery'));
  967. wp_enqueue_style('woocs_wselect', WOOCS_LINK . 'js/wselect/wSelect.css');
  968. }
  969.  
  970. //+++
  971. wp_enqueue_style('woocommerce-currency-switcher', WOOCS_LINK . 'css/front.css');
  972. wp_enqueue_script('woocommerce-currency-switcher', WOOCS_LINK . 'js/front.js', array('jquery'));
  973. //+++
  974. //trick for refreshing header cart after currency changing - code left just for history
  975. if (isset($_GET['currency'])) {
  976. //wp-content\plugins\woocommerce\includes\class-wc-cart.php
  977. //apply_filters('woocommerce_update_cart_action_cart_updated', true);
  978. //$this->current_currency = $_GET['currency'];
  979. //$_POST['update_cart'] = 1;
  980. //WC_Form_Handler::update_cart_action();
  981. //private function set_cart_cookies
  982. //wc_setcookie('woocommerce_items_in_cart', 0, time() - HOUR_IN_SECONDS);
  983. //wc_setcookie('woocommerce_cart_hash', '', time() - HOUR_IN_SECONDS);
  984. //do_action('woocommerce_cart_reset', WC()->cart, true);
  985. //do_action('woocommerce_calculate_totals', WC()->cart);
  986. }
  987.  
  988.  
  989. //if customer paying pending order from the front
  990. //checkout/order-pay/1044/?pay_for_order=true&key=order_55b764a4b7990
  991. if (isset($_GET['pay_for_order']) AND is_checkout_pay_page()) {
  992. if ($_GET['pay_for_order'] == 'true' AND isset($_GET['key'])) {
  993. $order_id = wc_get_order_id_by_order_key($_GET['key']);
  994. $currency = get_post_meta($order_id, '_order_currency', TRUE);
  995. $this->set_currency($currency);
  996. }
  997. }
  998.  
  999. //+++
  1000. //if we want to be paid in the basic currency - not multiple mode and in is_geoip_manipulation
  1001. if (!get_option('woocs_is_multiple_allowed', 0)) {
  1002. if (is_checkout() OR is_checkout_pay_page()) {
  1003. $this->reset_currency();
  1004. }
  1005. }
  1006.  
  1007.  
  1008. //logic hack for some cases when shipping for example is wrong in
  1009. //non multiple mode but customer doesn work allow pay in user selected currency
  1010. if ($this->is_multiple_allowed) {
  1011. if ((is_checkout() OR is_checkout_pay_page()) AND $this->bones['reset_in_multiple']) {
  1012. $this->reset_currency();
  1013. }
  1014. }
  1015.  
  1016.  
  1017.  
  1018. if ($this->force_pay_bygeoip_rules) {
  1019. if ((is_checkout() OR is_checkout_pay_page()) AND ! isset($_GET['key'])) {
  1020. $this->force_pay_bygeoip_rules();
  1021. }
  1022. }
  1023. }
  1024.  
  1025. public function woocommerce_checkout_update_order_review() {
  1026. if (!get_option('woocs_is_multiple_allowed', 0)) {
  1027. $this->reset_currency();
  1028. }
  1029. $this->force_pay_bygeoip_rules();
  1030. }
  1031.  
  1032. public function woocommerce_settings_tabs_array($tabs) {
  1033. $tabs['woocs'] = __('Currency', 'woocommerce-currency-switcher');
  1034. return $tabs;
  1035. }
  1036.  
  1037. public function print_plugin_options() {
  1038. if (isset($_POST['woocs_name']) AND ! empty($_POST['woocs_name'])) {
  1039. $result = array();
  1040. update_option('woocs_drop_down_view', $this->escape($_POST['woocs_drop_down_view']));
  1041. update_option('woocs_currencies_aggregator', $this->escape($_POST['woocs_currencies_aggregator']));
  1042. update_option('woocs_welcome_currency', $this->escape($_POST['woocs_welcome_currency']));
  1043. //***
  1044. update_option('woocs_is_multiple_allowed', (int) $_POST['woocs_is_multiple_allowed']);
  1045. update_option('woocs_is_geoip_manipulation', (int) $_POST['woocs_is_geoip_manipulation']);
  1046. if ((int) $_POST['woocs_is_multiple_allowed']) {
  1047. update_option('woocs_is_fixed_enabled', (int) $_POST['woocs_is_fixed_enabled']);
  1048. if ((int) $_POST['woocs_is_fixed_enabled']) {
  1049. update_option('woocs_force_pay_bygeoip_rules', (int) $_POST['woocs_force_pay_bygeoip_rules']);
  1050. } else {
  1051. update_option('woocs_force_pay_bygeoip_rules', 0);
  1052. }
  1053. } else {
  1054. update_option('woocs_is_fixed_enabled', 0);
  1055. update_option('woocs_force_pay_bygeoip_rules', 0);
  1056. }
  1057. //***
  1058. update_option('woocs_token', $this->escape($_POST['woocs_token']));
  1059. update_option('woocs_customer_signs', $this->escape($_POST['woocs_customer_signs']));
  1060. update_option('woocs_customer_price_format', $this->escape($_POST['woocs_customer_price_format']));
  1061. update_option('woocs_currencies_rate_auto_update', $this->escape($_POST['woocs_currencies_rate_auto_update']));
  1062. update_option('woocs_rate_auto_update_email', (int) $_POST['woocs_rate_auto_update_email']);
  1063. update_option('woocs_show_flags', (int) $_POST['woocs_show_flags']);
  1064. update_option('woocs_show_money_signs', (int) $_POST['woocs_show_money_signs']);
  1065. //update_option('woocs_use_curl', (int) $_POST['woocs_use_curl']);
  1066. update_option('woocs_storage', $this->escape($_POST['woocs_storage']));
  1067. if (isset($_POST['woocs_geo_rules'])) {
  1068. $woocs_geo_rules = array();
  1069. if (!empty($_POST['woocs_geo_rules'])) {
  1070. foreach ($_POST['woocs_geo_rules'] as $curr_key => $countries) {
  1071. $woocs_geo_rules[$this->escape($curr_key)] = array();
  1072. if (!empty($countries)) {
  1073. foreach ($countries as $curr) {
  1074. $woocs_geo_rules[$this->escape($curr_key)][] = $this->escape($curr);
  1075. }
  1076. }
  1077. }
  1078. }
  1079. update_option('woocs_geo_rules', $woocs_geo_rules);
  1080. } else {
  1081. update_option('woocs_geo_rules', '');
  1082. }
  1083. //update_option('woocs_use_geo_rules', (int) $_POST['woocs_use_geo_rules']);
  1084. update_option('woocs_hide_cents', (int) $_POST['woocs_hide_cents']);
  1085. update_option('woocs_price_info', (int) $_POST['woocs_price_info']);
  1086. update_option('woocs_no_cents', $this->escape($_POST['woocs_no_cents']));
  1087. update_option('woocs_restrike_on_checkout_page', (int) $_POST['woocs_restrike_on_checkout_page']);
  1088. update_option('woocs_show_approximate_amount', (int) $_POST['woocs_show_approximate_amount']);
  1089. update_option('woocs_shop_is_cached', (int) $_POST['woocs_shop_is_cached']);
  1090. update_option('woocs_woo_version', WOOCOMMERCE_VERSION);
  1091. //***
  1092. $cc = '';
  1093. foreach ($_POST['woocs_name'] as $key => $name) {
  1094. if (!empty($name)) {
  1095. $symbol = $this->escape($_POST['woocs_symbol'][$key]); //md5 encoded
  1096.  
  1097. foreach ($this->currency_symbols as $s) {
  1098. if (md5($s) == $symbol) {
  1099. $symbol = $s;
  1100. break;
  1101. }
  1102. }
  1103.  
  1104. $result[strtoupper($name)] = array(
  1105. 'name' => $name,
  1106. 'rate' => floatval($_POST['woocs_rate'][$key]),
  1107. 'symbol' => $symbol,
  1108. 'position' => (in_array($this->escape($_POST['woocs_position'][$key]), $this->currency_positions) ? $this->escape($_POST['woocs_position'][$key]) : $this->currency_positions[0]),
  1109. 'is_etalon' => (int) $_POST['woocs_is_etalon'][$key],
  1110. 'hide_cents' => (int) @$_POST['woocs_hide_cents'][$key],
  1111. 'decimals' => (int) @$_POST['woocs_decimals'][$key],
  1112. 'description' => $this->escape($_POST['woocs_description'][$key]),
  1113. 'flag' => $this->escape($_POST['woocs_flag'][$key]),
  1114. );
  1115.  
  1116. if ($_POST['woocs_rate'][$key] == 1) {
  1117. $cc = $name;
  1118. }
  1119. }
  1120. }
  1121.  
  1122. update_option('woocs', $result);
  1123. if (!empty($cc)) {
  1124. //set default currency for all woocommerce system
  1125. update_option('woocommerce_currency', $cc);
  1126. }
  1127. $this->init_currency_symbols();
  1128. //***
  1129. $this->rate_auto_update = $this->escape($_POST['woocs_currencies_rate_auto_update']);
  1130. $this->wp_cron_period = $this->get_woocs_cron_schedules($this->rate_auto_update);
  1131. $this->woocs_wpcron_init(true);
  1132. $this->make_rates_auto_update(true);
  1133. //$this->reset_currency();
  1134. }
  1135. //+++
  1136. //wp_enqueue_style('open_sans_font', 'https://fonts.googleapis.com/css?family=Open+Sans');
  1137. wp_enqueue_script('media-upload');
  1138. wp_enqueue_style('thickbox');
  1139. wp_enqueue_script('thickbox');
  1140. wp_enqueue_script('jquery');
  1141. wp_enqueue_script('jquery-ui-core');
  1142. wp_enqueue_script('jquery-ui-tabs');
  1143. wp_enqueue_script('woocommerce-currency-switcher-options', WOOCS_LINK . 'js/options.js', array('jquery', 'jquery-ui-core', 'jquery-ui-sortable'));
  1144.  
  1145. $args = array();
  1146. $args['currencies'] = $this->get_currencies();
  1147. if ($this->is_use_geo_rules()) {
  1148. $args['geo_rules'] = $this->get_geo_rules();
  1149. }
  1150. echo $this->render_html(WOOCS_PATH . 'views/plugin_options.php', $args);
  1151. }
  1152.  
  1153. public function get_drop_down_view() {
  1154. return apply_filters('woocs_drop_down_view', get_option('woocs_drop_down_view', 'ddslick'));
  1155. //return get_option('woocs_drop_down_view', 'ddslick');
  1156. }
  1157.  
  1158. public function get_currencies() {
  1159. //static $currencies = array();
  1160. //$_POST['woocs_name'] - reinit after saving
  1161. /*
  1162. if (!empty($currencies) AND ! isset($_POST['woocs_name']))
  1163. {
  1164. return $currencies;
  1165. }
  1166. *
  1167. */
  1168.  
  1169. $default = array(
  1170. 'USD' => array(
  1171. 'name' => 'USD',
  1172. 'rate' => 1,
  1173. 'symbol' => '&#36;',
  1174. 'position' => 'right',
  1175. 'is_etalon' => 1,
  1176. 'description' => 'USA dollar',
  1177. 'hide_cents' => 0,
  1178. 'flag' => '',
  1179. ),
  1180. 'EUR' => array(
  1181. 'name' => 'EUR',
  1182. 'rate' => 0.89,
  1183. 'symbol' => '&euro;',
  1184. 'position' => 'left_space',
  1185. 'is_etalon' => 0,
  1186. 'description' => 'European Euro',
  1187. 'hide_cents' => 0,
  1188. 'flag' => '',
  1189. )
  1190. );
  1191.  
  1192. $currencies = get_option('woocs', $default);
  1193. $currencies = apply_filters('woocs_currency_data_manipulation', $currencies);
  1194.  
  1195. /*
  1196. //http://currency-switcher.com/how-to-manipulate-with-currencies-rates/
  1197. foreach ($currencies as $key => $value)
  1198. {
  1199. if($key == 'EUR'){
  1200. $currencies[$key]['rate']=$currencies[$key]['rate']+0.025;
  1201. break;
  1202. }
  1203. }
  1204. */
  1205.  
  1206. if (empty($currencies) OR ! is_array($currencies)) {
  1207. $currencies = $default;
  1208. }
  1209.  
  1210. if (count($currencies) < 2) {
  1211. $currencies = $default;
  1212. }
  1213.  
  1214. if (count($currencies) !== 2) {
  1215. $currencies = array_slice($currencies, 0, 2);
  1216. }
  1217.  
  1218.  
  1219. return $currencies;
  1220. }
  1221.  
  1222. public function get_geo_rules() {
  1223. return get_option('woocs_geo_rules', array());
  1224. }
  1225.  
  1226. public function is_use_geo_rules() {
  1227. //$is = get_option('woocs_use_geo_rules', 0);
  1228. $is = true; //from v.2.1.8 always enabled
  1229. $isset = class_exists('WC_Geolocation');
  1230.  
  1231. return ($is && $isset);
  1232. }
  1233.  
  1234. //need for paypal currencies supporting
  1235. public function enable_custom_currency($currency_array) {
  1236. //https://developer.paypal.com/docs/classic/api/currency_codes/
  1237. //includes\gateways\paypal\class-wc-gateway-paypal.php => woo func
  1238. //function is_valid_for_use() =>Check if this gateway is enabled and available in the user's country
  1239. $currency_array[] = 'usd';
  1240. $currency_array[] = 'aud';
  1241. $currency_array[] = 'brl';
  1242. $currency_array[] = 'cad';
  1243. $currency_array[] = 'czk';
  1244. $currency_array[] = 'dkk';
  1245. $currency_array[] = 'eur';
  1246. $currency_array[] = 'hkd';
  1247. $currency_array[] = 'huf';
  1248. $currency_array[] = 'ils';
  1249. $currency_array[] = 'jpy';
  1250. $currency_array[] = 'myr';
  1251. $currency_array[] = 'mxn';
  1252. $currency_array[] = 'nok';
  1253. $currency_array[] = 'nzd';
  1254. $currency_array[] = 'php';
  1255. $currency_array[] = 'pln';
  1256. $currency_array[] = 'gbp';
  1257. $currency_array[] = 'rub';
  1258. $currency_array[] = 'sgd';
  1259. $currency_array[] = 'sek';
  1260. $currency_array[] = 'chf';
  1261. $currency_array[] = 'twd';
  1262. $currency_array[] = 'thb';
  1263. $currency_array[] = 'try';
  1264. $currency_array = array_map('strtoupper', $currency_array);
  1265. return $currency_array;
  1266. }
  1267.  
  1268. public function woocommerce_currency_symbol($currency_symbol) {
  1269.  
  1270. $currencies = $this->get_currencies();
  1271.  
  1272. if (!isset($currencies[$this->current_currency])) {
  1273. $this->reset_currency();
  1274. }
  1275.  
  1276. return $currencies[$this->current_currency]['symbol'];
  1277. }
  1278.  
  1279. public function get_woocommerce_currency() {
  1280. return $this->current_currency;
  1281. }
  1282.  
  1283. //work in for multiple mode only from woocommerce 2.4
  1284. //wp-content\plugins\woocommerce\includes\class-wc-product-variable.php #303
  1285. public function woocommerce_variation_prices($prices_array) {
  1286. /*
  1287. if (isset($_REQUEST['woocs_woocommerce_available_variation_is']))
  1288. {
  1289. echo '<pre>';
  1290. print_r($prices_array);
  1291. echo '</pre>';
  1292. }
  1293. */
  1294.  
  1295. $current_currency = $this->current_currency;
  1296.  
  1297. //***
  1298.  
  1299. if (in_array($current_currency, $this->no_cents)/* OR $currencies[$this->current_currency]['hide_cents'] == 1 */) {
  1300. $precision = 0;
  1301. } else {
  1302. if ($current_currency != $this->default_currency) {
  1303. $precision = $this->get_currency_price_num_decimals($current_currency, $this->price_num_decimals);
  1304. } else {
  1305. $precision = $this->get_currency_price_num_decimals($this->default_currency, $this->price_num_decimals);
  1306. }
  1307. }
  1308.  
  1309. //***
  1310.  
  1311. if (!empty($prices_array) AND is_array($prices_array)) {
  1312. //remove sale prices if they equal to regular prices
  1313. foreach ($prices_array['regular_price'] as $key => $value) {
  1314. if ($value === $prices_array['sale_price'][$key]) {
  1315. unset($prices_array['sale_price'][$key]);
  1316. }
  1317. }
  1318.  
  1319. //***
  1320.  
  1321. foreach ($prices_array as $key => $values) {
  1322. if (!empty($values)) {
  1323. foreach ($values as $product_id => $price) {
  1324.  
  1325. $type = 'regular';
  1326. if ($key === 'sale_price' OR $key === 'price') {
  1327. $type = 'sale';
  1328. }
  1329.  
  1330. $is_price_custom = false;
  1331.  
  1332. if ($this->is_fixed_enabled AND $this->fixed->is_exists($product_id, $current_currency, $type)) {
  1333. $tmp = number_format(floatval($this->fixed->get_value($product_id, $current_currency, $type)), $precision, $this->decimal_sep, '');
  1334.  
  1335. if ((int) $tmp !== -1) {
  1336. $prices_array[$key][$product_id] = $tmp;
  1337. $is_price_custom = true;
  1338. }
  1339. }
  1340.  
  1341.  
  1342. if ($this->is_geoip_manipulation AND ! $is_price_custom) {
  1343. $product = (object) array('id' => $product_id);
  1344. $price = $this->_get_product_geo_price($product, $price);
  1345. }
  1346.  
  1347.  
  1348. if (!$is_price_custom) {
  1349. //if ($current_currency != $this->default_currency)
  1350. {
  1351. $prices_array[$key][$product_id] = apply_filters('woocs_woocommerce_variation_prices', number_format(floatval($this->woocs_exchange_value(floatval($price))), $precision, $this->decimal_sep, ''));
  1352. }
  1353. }
  1354. }
  1355. }
  1356. }
  1357. }
  1358.  
  1359. //***
  1360. /*
  1361. if (!$is_price_custom)
  1362. {
  1363. if ($this->current_currency != $this->default_currency)
  1364. {
  1365. if (!empty($prices_array) AND is_array($prices_array))
  1366. {
  1367. foreach ($prices_array as $key => $values)
  1368. {
  1369. foreach ($values as $k => $v)
  1370. {
  1371. //$prices_array[$key][$k] = $this->woocs_exchange_value(floatval($v));
  1372.  
  1373. }
  1374. }
  1375. }
  1376. }
  1377. }
  1378. */
  1379. //*** lets sort arrays by values to avoid wrong price displaying on the front
  1380. if (!empty($prices_array) AND is_array($prices_array)) {
  1381. foreach ($prices_array as $key => $arrvals) {
  1382. asort($arrvals);
  1383. $prices_array[$key] = $arrvals;
  1384. }
  1385. }
  1386. //***
  1387. //another way displaing of price range is not correct
  1388. if (empty($prices_array['sale_price'])) {
  1389. if (isset($prices_array['regular_price'])) {
  1390. $prices_array['price'] = $prices_array['regular_price'];
  1391. }
  1392. }
  1393. //***
  1394. //echo '<pre>' . print_r($prices_array, true) . '</pre>';
  1395. return $prices_array;
  1396. }
  1397.  
  1398. public function woocommerce_get_variation_prices_hash($price_hash, $product, $display) {
  1399. //***
  1400. }
  1401.  
  1402. /*
  1403. public function raw_woocommerce_price_regular($price, $product)
  1404. {
  1405. $this->raw_woocommerce_price($price, $product, 'regular');
  1406. }
  1407. */
  1408.  
  1409. public function raw_woocommerce_price($price, $product = NULL) {
  1410.  
  1411. if (isset($_REQUEST['woocs_block_price_hook'])) {
  1412. return $price;
  1413. }
  1414.  
  1415. //to avoid 'Free!' text if price is zero - buy button will be hidden
  1416. if (empty($price)) {
  1417. //return "";//doesn work correctly for the fixed prices
  1418. }
  1419.  
  1420. //***
  1421.  
  1422. $currencies = $this->get_currencies();
  1423.  
  1424.  
  1425. if (in_array($this->current_currency, $this->no_cents)/* OR $currencies[$this->current_currency]['hide_cents'] == 1 */) {
  1426. $precision = 0;
  1427. } else {
  1428. if ($this->current_currency != $this->default_currency) {
  1429. $precision = $this->get_currency_price_num_decimals($this->current_currency, $this->price_num_decimals);
  1430. } else {
  1431. $precision = $this->get_currency_price_num_decimals($this->default_currency, $this->price_num_decimals);
  1432. }
  1433. }
  1434.  
  1435. //***
  1436. $is_price_custom = false;
  1437. if ($this->is_fixed_enabled) {
  1438. if ($this->is_multiple_allowed AND $product !== NULL AND is_object($product)) {
  1439.  
  1440. //if (isset($product->variation_id))
  1441. if ($product->is_type('variation')) {
  1442. $tmp_val = $this->_get_product_fixed_price($product, 'variation', $price, $precision);
  1443. } else {
  1444. $tmp_val = $this->_get_product_fixed_price($product, 'single', $price, $precision);
  1445. }
  1446.  
  1447. if ((int) $tmp_val !== -1) {
  1448. $price = apply_filters('woocs_fixed_raw_woocommerce_price', $tmp_val, $product);
  1449. $is_price_custom = true;
  1450. }
  1451.  
  1452. //$price=rand(1,99);
  1453. //$is_price_custom = true;
  1454. }
  1455. }
  1456. //***
  1457. if ($this->is_geoip_manipulation AND ! $is_price_custom) {
  1458. if ($product !== NULL) {
  1459.  
  1460. if (version_compare(WOOCOMMERCE_VERSION, '2.7', '>=')) {
  1461. try {
  1462. $product_emulator = (object) array('id' => $product->get_id());
  1463. } catch (Exception $e) {
  1464. //print_r($e);
  1465. }
  1466. } else {
  1467. if (isset($product->variation_id)) {
  1468. $product_emulator = (object) array('id' => $product->variation_id);
  1469. } else {
  1470. $product_emulator = (object) array('id' => $product->id);
  1471. }
  1472. }
  1473.  
  1474.  
  1475. $price = $this->_get_product_geo_price($product_emulator, $price);
  1476. }
  1477. }
  1478.  
  1479. //***
  1480.  
  1481. if (!$is_price_custom) {
  1482. if ($this->current_currency != $this->default_currency) {
  1483. //Edited this line to set default convertion of currency
  1484. if (isset($currencies[$this->current_currency]) AND $currencies[$this->current_currency] != NULL) {
  1485. $price = number_format(floatval($price * $currencies[$this->current_currency]['rate']), $precision, $this->decimal_sep, '');
  1486. } else {
  1487. $price = number_format(floatval($price * $currencies[$this->default_currency]['rate']), $precision, $this->decimal_sep, '');
  1488. }
  1489. }
  1490. }
  1491.  
  1492. return apply_filters('woocs_raw_woocommerce_price', $price);
  1493.  
  1494. //some hints for price rounding
  1495. //http://stackoverflow.com/questions/11692770/rounding-to-nearest-50-cents
  1496. //$price = round($price * 2, 0) / 2;
  1497. //return round ( $price , 0 ,PHP_ROUND_HALF_EVEN );
  1498. //return number_format ($price, $this->price_num_decimals, $this->decimal_sep, $this->thousands_sep);
  1499. }
  1500.  
  1501. //fix for only woo>=2.7 when multiple mode is activated and price is not sale - price still crossed out
  1502. public function raw_woocommerce_price_sale($price, $product = NULL) {
  1503.  
  1504. if (!$this->is_multiple_allowed) {
  1505. return $this->raw_woocommerce_price($price, $product);
  1506. }
  1507.  
  1508. if ($this->is_multiple_allowed) {
  1509. if ($product !== NULL) {
  1510. if ($product->get_sale_price('edit') > 0) {
  1511. return $this->raw_woocommerce_price($price, $product);
  1512. }
  1513. }
  1514. }
  1515. }
  1516.  
  1517. //+++++++++++++++++++++++++++++ START: USES ONLY FOR WOO > 2.7 AS FIX ON THE CHEKOUT FOR VARIABLE PRODUCTS ++++++++++++++++++++++++++++++++
  1518. //works only in multiple allowed mode
  1519. public function woocommerce_cart_product_subtotal($product_subtotal, $product, $quantity, $cart) {
  1520.  
  1521. if ($product->post_type == 'product_variation') {
  1522. //$product_subtotal = $this->raw_woocommerce_price($product->get_price()*$quantity, $product);
  1523. $product_subtotal = $this->wc_price($product->get_price() * $quantity);
  1524. }
  1525.  
  1526. return $product_subtotal;
  1527. }
  1528.  
  1529. public function woocommerce_cart_product_price($price, $product) {
  1530.  
  1531. if ($product->post_type == 'product_variation') {
  1532. $price = $this->wc_price($product->get_price());
  1533. }
  1534.  
  1535. return $price;
  1536. }
  1537.  
  1538. public function woocommerce_cart_subtotal2($cart_subtotal, $compound, $cart) {
  1539. /*
  1540. echo '<pre>';
  1541. print_r($cart);
  1542. echo '</pre>';
  1543. */
  1544. if (!empty($cart) AND isset($cart->cart_contents)) {
  1545. if (!empty($cart->cart_contents)) {
  1546. $cart_subtotal = 0;
  1547. foreach ($cart->cart_contents as $ci) {
  1548. if ($ci['variation_id'] > 0) {
  1549. $cart_subtotal += $this->woocs_exchange_value($ci['line_total']);
  1550. } else {
  1551. $cart_subtotal += $ci['line_total'];
  1552. }
  1553. }
  1554. }
  1555. }
  1556.  
  1557. return $this->wc_price($cart_subtotal, false);
  1558. }
  1559.  
  1560. public function woocommerce_cart_contents_total2($cart_contents_total) {
  1561. return 101;
  1562. }
  1563.  
  1564. //+++++++++++++++++++++++++++++ FIINISH: USES ONLY FOR WOO > 2.7 AS FIX ON THE CHEKOUT FOR VARIABLE PRODUCTS ++++++++++++++++++++++++++++++++
  1565. //for tooltip
  1566. private function _get_min_max_variation_prices($product, $current_currency) {
  1567. $currencies = $this->get_currencies();
  1568. $prices_array = $product->get_variation_prices();
  1569. $var_products_ids = array_keys($prices_array['regular_price']);
  1570. $prices_array = array();
  1571. if (!empty($var_products_ids)) {
  1572. foreach ($var_products_ids as $var_prod_id) {
  1573.  
  1574. $is_price_custom = false;
  1575. $regular_price = (float) get_post_meta($var_prod_id, '_regular_price', true);
  1576. $sale_price = (float) get_post_meta($var_prod_id, '_sale_price', true);
  1577.  
  1578. //+++
  1579.  
  1580. if ($this->is_fixed_enabled) {
  1581. $type = 'regular';
  1582. $fixed_regular_price = -1;
  1583. $fixed_sale_price = -1;
  1584.  
  1585. if ($this->fixed->is_exists($var_prod_id, $current_currency, $type)) {
  1586. $tmp = $this->fixed->get_value($var_prod_id, $current_currency, $type);
  1587. if ((int) $tmp !== -1) {
  1588. $fixed_regular_price = $tmp;
  1589. }
  1590. }
  1591.  
  1592. $type = 'sale';
  1593. if ($this->fixed->is_exists($var_prod_id, $current_currency, $type)) {
  1594. $tmp = $this->fixed->get_value($var_prod_id, $current_currency, $type);
  1595. if ((int) $tmp !== -1) {
  1596. $fixed_sale_price = $tmp;
  1597. }
  1598. }
  1599.  
  1600. if ((int) $fixed_sale_price !== -1) {
  1601. $prices_array[] = $fixed_sale_price;
  1602. $is_price_custom = true;
  1603. } else {
  1604. if ((int) $fixed_regular_price !== -1) {
  1605. $prices_array[] = $fixed_regular_price;
  1606. $is_price_custom = true;
  1607. }
  1608. }
  1609. }
  1610.  
  1611.  
  1612. if ($this->is_geoip_manipulation AND ! $is_price_custom) {
  1613. $product = (object) array('id' => $var_prod_id);
  1614. $regular_price = floatval($this->_get_product_geo_price($product, $regular_price));
  1615. $sale_price = floatval($this->_get_product_geo_price($product, $sale_price));
  1616. //echo $regular_price . '~~~' . $sale_price . '+++';
  1617. }
  1618.  
  1619.  
  1620. if (!$is_price_custom) {
  1621. $regular_price = floatval($currencies[$current_currency]['rate'] * $regular_price);
  1622. $sale_price = floatval($currencies[$current_currency]['rate'] * $sale_price);
  1623.  
  1624. if ($sale_price > 0) {
  1625. $prices_array[] = $sale_price;
  1626. } else {
  1627. $prices_array[] = $regular_price;
  1628. }
  1629. }
  1630. }
  1631. }
  1632.  
  1633. //***
  1634.  
  1635. if (!empty($prices_array)) {
  1636. foreach ($prices_array as $key => $value) {
  1637. if (floatval($value) <= 0) {
  1638. unset($prices_array[$key]);
  1639. }
  1640. }
  1641.  
  1642. //echo '<pre>' . print_r($prices_array, true) . '</pre>';
  1643. if (!empty($prices_array)) {
  1644. return array('min' => min($prices_array), 'max' => max($prices_array));
  1645. }
  1646. }
  1647.  
  1648.  
  1649. return array();
  1650. }
  1651.  
  1652. //$product_type - single, variation - $product->id, $product->variation_id
  1653. public function _get_product_fixed_price($product, $product_type, $price, $precision = 2, $type = NULL) {
  1654.  
  1655. if (version_compare(WOOCOMMERCE_VERSION, '2.7', '>=')) {
  1656. $product_id = $product->get_id();
  1657. } else {
  1658. if ($product_type == 'single') {
  1659. $product_id = $product->id;
  1660. } else {
  1661. $product_id = $product->variation_id;
  1662. }
  1663. }
  1664.  
  1665. //***
  1666. if (!$type) {
  1667. $type = $this->fixed->get_price_type($product, $price);
  1668. }
  1669.  
  1670. $is_empty = $this->fixed->is_empty($product_id, $this->current_currency, $type);
  1671. $is_exists = $this->fixed->is_exists($product_id, $this->current_currency, $type);
  1672.  
  1673. //if sale field is empty BUT regular not, in such case price exists and it is regular
  1674. if ($type == 'sale' AND $is_empty) {
  1675. $type = 'regular';
  1676. $is_exists = $this->fixed->is_exists($product_id, $this->current_currency, $type);
  1677. $is_empty = $this->fixed->is_empty($product_id, $this->current_currency, $type);
  1678. }
  1679.  
  1680. if ($is_exists AND ! $is_empty) {
  1681. return number_format(floatval($this->fixed->get_value($product_id, $this->current_currency, $type)), $precision, $this->decimal_sep, '');
  1682. }
  1683.  
  1684.  
  1685. return -1;
  1686. }
  1687.  
  1688. private function _get_product_geo_price($product, $price, $type = NULL, $is_array = false) {
  1689. $is_price_custom = false;
  1690. if ($product !== NULL AND is_object($product)) {
  1691. if (version_compare(WOOCOMMERCE_VERSION, '2.7', '>=')) {
  1692. if (method_exists($product, 'get_id')) {
  1693. $product_id = $product->get_id();
  1694. } else {
  1695. $product_id = $product->id;
  1696. }
  1697. } else {
  1698. $product_id = $product->id;
  1699. }
  1700.  
  1701. if (!$type) {
  1702. $type = $this->fixed->get_price_type($product, $price);
  1703. }
  1704. /*
  1705. static $products_data = array();
  1706. if (isset($products_data[$product_id])) {
  1707. if ($products_data[$product_id] < $price) {
  1708. $type = 'regular';
  1709. }else{
  1710. $type = 'sale';
  1711. }
  1712. } else {
  1713. $products_data[$product_id] = $price;
  1714. $type = 'sale';
  1715. }
  1716. */
  1717.  
  1718. $product_geo_data = $this->fixed->get_product_geo_data($product_id);
  1719.  
  1720. if (isset($product_geo_data[$type . '_price_geo'])) {
  1721. if (!empty($product_geo_data[$type . '_price_geo'])) {
  1722. $user_country = $this->storage->get_val('woocs_user_country');
  1723. //$user_currency = $this->get_currency_by_country($country);
  1724. if (!empty($user_country)) {
  1725. if (!empty($product_geo_data['price_geo_countries'])) {
  1726. $price_key = '';
  1727. foreach ($product_geo_data['price_geo_countries'] as $block_key => $countries_codes) {
  1728. if (!empty($countries_codes)) {
  1729. foreach ($countries_codes as $country_code) {
  1730. if ($country_code === $user_country) {
  1731. $price_key = $block_key;
  1732. break(2);
  1733. }
  1734. }
  1735. }
  1736. }
  1737.  
  1738. //***
  1739.  
  1740. if (isset($product_geo_data[$type . '_price_geo'][$price_key])) {
  1741. $price = $product_geo_data[$type . '_price_geo'][$price_key];
  1742. $is_price_custom = true;
  1743. }
  1744. }
  1745. }
  1746. }
  1747. }
  1748. }
  1749.  
  1750. if ($is_array) {
  1751. return array($price, $is_price_custom);
  1752. }
  1753.  
  1754. return $price;
  1755. }
  1756.  
  1757. public function get_welcome_currency() {
  1758. return get_option('woocs_welcome_currency');
  1759. }
  1760.  
  1761. public function get_customer_signs() {
  1762. $signs = array();
  1763. $data = get_option('woocs_customer_signs', '');
  1764. if (!empty($data)) {
  1765. $data = explode(',', $data);
  1766. if (!empty($data) AND is_array($data)) {
  1767. $signs = $data;
  1768. }
  1769. }
  1770. return $signs;
  1771. }
  1772.  
  1773. public function get_checkout_page_id() {
  1774. return (int) get_option('woocommerce_checkout_page_id');
  1775. }
  1776.  
  1777. public function force_pay_bygeoip_rules() {
  1778. //$use_geo_rules = get_option('woocs_use_geo_rules', 0);
  1779. $use_geo_rules = true;
  1780. if ($this->is_multiple_allowed AND $this->force_pay_bygeoip_rules AND $use_geo_rules) {
  1781. $country = $this->storage->get_val('woocs_user_country');
  1782. $user_currency = $this->get_currency_by_country($country);
  1783. if (!empty($user_currency)) {
  1784. //$user_currency is empty its mean that current country is not in geo ip rules
  1785. $this->set_currency($user_currency);
  1786. }
  1787. do_action('woocs_force_pay_bygeoip_rules', $country, $user_currency, $this->current_currency);
  1788. }
  1789. }
  1790.  
  1791. public function woocommerce_price_format() {
  1792. $currencies = $this->get_currencies();
  1793. $currency_pos = 'left';
  1794. if (isset($currencies[$this->current_currency])) {
  1795. $currency_pos = $currencies[$this->current_currency]['position'];
  1796. }
  1797. $format = '%1$s%2$s';
  1798. switch ($currency_pos) {
  1799. case 'left' :
  1800. $format = '%1$s%2$s';
  1801. break;
  1802. case 'right' :
  1803. $format = '%2$s%1$s';
  1804. break;
  1805. case 'left_space' :
  1806. $format = '%1$s&nbsp;%2$s';
  1807. break;
  1808. case 'right_space' :
  1809. $format = '%2$s&nbsp;%1$s';
  1810. break;
  1811. }
  1812.  
  1813. return apply_filters('woocs_price_format', $format, $currency_pos);
  1814. }
  1815.  
  1816. //[woocs]
  1817. public function woocs_shortcode($args) {
  1818. if (empty($args)) {
  1819. $args = array();
  1820. }
  1821. return $this->render_html(WOOCS_PATH . 'views/shortcodes/woocs.php', $args);
  1822. }
  1823.  
  1824. //[woocs_converter exclude="GBP,AUD" precision=2]
  1825. public function woocs_converter($args) {
  1826. if (empty($args)) {
  1827. $args = array();
  1828. }
  1829. return $this->render_html(WOOCS_PATH . 'views/shortcodes/woocs_converter.php', $args);
  1830. }
  1831.  
  1832. //[woocs_rates exclude="GBP,AUD" precision=2]
  1833. public function woocs_rates($args) {
  1834. if (empty($args)) {
  1835. $args = array();
  1836. }
  1837. return $this->render_html(WOOCS_PATH . 'views/shortcodes/woocs_rates.php', $args);
  1838. }
  1839.  
  1840. //[woocs_show_current_currency text="" currency="" flag=1 code=1]
  1841. public function woocs_show_current_currency($atts) {
  1842. $currencies = $this->get_currencies();
  1843. extract(shortcode_atts(array(
  1844. 'text' => __('Current currency is:', 'woocommerce-currency-switcher'),
  1845. 'currency' => $this->current_currency,
  1846. 'flag' => 1,
  1847. 'code' => 1,
  1848. ), $atts));
  1849.  
  1850. $args = array();
  1851. $args['currencies'] = $currencies;
  1852. $args['text'] = $text;
  1853. $args['currency'] = $currency;
  1854. $args['flag'] = $flag;
  1855. $args['code'] = $code;
  1856. return $this->render_html(WOOCS_PATH . 'views/shortcodes/woocs_show_current_currency.php', $args);
  1857. }
  1858.  
  1859. //[woocs_show_custom_price value=20] -> value should be in default currency
  1860. public function woocs_show_custom_price($atts) {
  1861. extract(shortcode_atts(array('value' => 0), $atts));
  1862. $currencies = $this->get_currencies();
  1863. $convert = true;
  1864. $_REQUEST['woocs_show_custom_price'] = TRUE;
  1865. $wc_price = $this->wc_price($value, $convert, array('currency' => $currencies[$this->current_currency]['name']));
  1866. unset($_REQUEST['woocs_show_custom_price']);
  1867. return $wc_price;
  1868. }
  1869.  
  1870. //for geo ip demo
  1871. public function woocs_geo_hello($atts = '') {
  1872. $pd = array();
  1873. $countries = array();
  1874. $text = '';
  1875. if (class_exists('WC_Geolocation')) {
  1876. $c = new WC_Countries();
  1877. $countries = $c->get_countries();
  1878. $pd = WC_Geolocation::geolocate_ip();
  1879. }
  1880. if (!empty($pd) AND ! empty($countries)) {
  1881. $text = '<span style="color:green; font-size:18px; font-weight:normal; line-height:0;">' . sprintf(__('Your country is: <b>%s</b>. <br /><i style="color:black; font-size:11px; font-weight:normal;">(defined by woocommerce GeoIP functionality)</i>', 'woocommerce-currency-switcher'), $countries[$pd['country']]) . '</span>';
  1882. } else {
  1883. $text = '<i style="color:red; font-size:18px; font-weight:normal;">' . __('Your country is not defined! Troubles with GeoIp service.', 'woocommerce-currency-switcher') . '</i>';
  1884. }
  1885.  
  1886. return $text;
  1887. }
  1888.  
  1889. //http://stackoverflow.com/questions/6918623/curlopt-followlocation-cannot-be-activated
  1890. private function file_get_contents_curl($url) {
  1891. $ch = curl_init();
  1892.  
  1893. curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
  1894. curl_setopt($ch, CURLOPT_HEADER, 0);
  1895. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1896. curl_setopt($ch, CURLOPT_URL, $url);
  1897. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  1898.  
  1899. $data = curl_exec($ch);
  1900. curl_close($ch);
  1901.  
  1902. return $data;
  1903. }
  1904.  
  1905. //ajax
  1906. public function get_rate() {
  1907. $is_ajax = true;
  1908. if (isset($_REQUEST['no_ajax'])) {
  1909. $is_ajax = false;
  1910. }
  1911.  
  1912. //***
  1913. //http://en.wikipedia.org/wiki/ISO_4217
  1914. $mode = get_option('woocs_currencies_aggregator', 'yahoo');
  1915. $request = "";
  1916. //$woocs_use_curl = (int) get_option('woocs_use_curl', 0);
  1917. $woocs_use_curl = 1;
  1918. switch ($mode) {
  1919. case 'yahoo':
  1920. //http://www.idiotinside.com/2015/01/28/create-a-currency-converter-in-php-python-javascript-and-jquery-using-yahoo-currency-api/
  1921. $yql_base_url = "http://query.yahooapis.com/v1/public/yql";
  1922. $yql_query = 'select * from yahoo.finance.xchange where pair in ("' . $this->default_currency . $this->escape($_REQUEST['currency_name']) . '")';
  1923. $yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
  1924. $yql_query_url .= "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
  1925. //***
  1926. if (function_exists('curl_init') AND $woocs_use_curl) {
  1927. $res = $this->file_get_contents_curl($yql_query_url);
  1928. } else {
  1929. $res = file_get_contents($yql_query_url);
  1930. }
  1931. //***
  1932. $yql_json = json_decode($res, true);
  1933. $request = (float) $yql_json['query']['results']['rate']['Rate'];
  1934.  
  1935.  
  1936. break;
  1937.  
  1938. case 'publicapicpr':
  1939. $token = $this->token ;
  1940. $from_Currency = urlencode($this->default_currency);
  1941. if($from_Currency == 'NGN'){
  1942. $from_Currency = $this->escape($_REQUEST['currency_name']);
  1943. }
  1944. $api_url = "https://publicapi.com.ng/api/$token/pcr/". $from_Currency;
  1945. if (function_exists('curl_init') AND $woocs_use_curl) {
  1946. $res = $this->file_get_contents_curl($api_url);
  1947. } else {
  1948. $res = file_get_contents($api_url);
  1949. }
  1950. $pcr_json = json_decode($res, true);
  1951. $request = (float) $pcr_json['sell'];
  1952. if($from_Currency == 'NGN'){
  1953. $request = (float) 1/$request;
  1954.  
  1955. }
  1956.  
  1957. break;
  1958.  
  1959. case 'google':
  1960. $amount = urlencode(1);
  1961. $from_Currency = urlencode($this->default_currency);
  1962. $to_Currency = urlencode($this->escape($_REQUEST['currency_name']));
  1963. $url = "http://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency";
  1964.  
  1965. if (function_exists('curl_init') AND $woocs_use_curl) {
  1966. $html = $this->file_get_contents_curl($url);
  1967. } else {
  1968. $html = file_get_contents($url);
  1969. }
  1970.  
  1971. preg_match_all('/<span class=bld>(.*?)<\/span>/s', $html, $matches);
  1972. if (isset($matches[1][0])) {
  1973. $request = floatval($matches[1][0]);
  1974. } else {
  1975. $request = sprintf(__("no data for %s", 'woocommerce-currency-switcher'), $this->escape($_REQUEST['currency_name']));
  1976. }
  1977.  
  1978. break;
  1979.  
  1980. case 'privatbank':
  1981. //https://api.privatbank.ua/#p24/exchange
  1982. $url = 'https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=4'; //4,5
  1983.  
  1984. if (function_exists('curl_init') AND $woocs_use_curl) {
  1985. $res = $this->file_get_contents_curl($url);
  1986. } else {
  1987. $res = file_get_contents($url);
  1988. }
  1989.  
  1990. $currency_data = json_decode($res, true);
  1991. $rates = array();
  1992.  
  1993.  
  1994. if (!empty($currency_data)) {
  1995. foreach ($currency_data as $c) {
  1996. if ($c['base_ccy'] == 'UAH') {
  1997. $rates[$c['ccy']] = floatval($c['sale']);
  1998. }
  1999. }
  2000. }
  2001.  
  2002.  
  2003. //***
  2004.  
  2005. if (!empty($rates)) {
  2006.  
  2007. if ($this->default_currency != 'UAH') {
  2008. if ($_REQUEST['currency_name'] != 'UAH') {
  2009. if (isset($_REQUEST['currency_name'])) {
  2010. $request = floatval($rates[$this->default_currency] / ($rates[$this->escape($_REQUEST['currency_name'])]));
  2011. } else {
  2012. $request = sprintf(__("no data for %s", 'woocommerce-currency-switcher'), $this->escape($_REQUEST['currency_name']));
  2013. }
  2014. } else {
  2015. $request = 1 / (1 / $rates[$this->default_currency]);
  2016. }
  2017. } else {
  2018. if ($_REQUEST['currency_name'] != 'UAH') {
  2019. $request = 1 / $rates[$_REQUEST['currency_name']];
  2020. } else {
  2021. $request = 1;
  2022. }
  2023. }
  2024. } else {
  2025. $request = sprintf(__("no data for %s", 'woocommerce-currency-switcher'), $this->escape($_REQUEST['currency_name']));
  2026. }
  2027.  
  2028. //***
  2029.  
  2030. if (!$request) {
  2031. $request = sprintf(__("no data for %s", 'woocommerce-currency-switcher'), $this->escape($_REQUEST['currency_name']));
  2032. }
  2033.  
  2034.  
  2035. break;
  2036.  
  2037.  
  2038.  
  2039. case 'ecb':
  2040. $url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
  2041.  
  2042. if (function_exists('curl_init') AND $woocs_use_curl) {
  2043. $res = $this->file_get_contents_curl($url);
  2044. } else {
  2045. $res = file_get_contents($url);
  2046. }
  2047.  
  2048. $currency_data = simplexml_load_string($res);
  2049. $rates = array();
  2050. if (empty($currency_data->Cube->Cube)) {
  2051. $request = sprintf(__("no data for %s", 'woocommerce-currency-switcher'), $this->escape($_REQUEST['currency_name']));
  2052. break;
  2053. }
  2054.  
  2055.  
  2056.  
  2057. foreach ($currency_data->Cube->Cube->Cube as $xml) {
  2058. $att = (array) $xml->attributes();
  2059. $rates[$att['@attributes']['currency']] = floatval($att['@attributes']['rate']);
  2060. }
  2061.  
  2062.  
  2063. //***
  2064.  
  2065. if (!empty($rates)) {
  2066.  
  2067. if ($this->default_currency != 'EUR') {
  2068. if ($_REQUEST['currency_name'] != 'EUR') {
  2069. if (isset($_REQUEST['currency_name'])) {
  2070. $request = floatval($rates[$this->escape($_REQUEST['currency_name'])] / $rates[$this->default_currency]);
  2071. } else {
  2072. $request = sprintf(__("no data for %s", 'woocommerce-currency-switcher'), $this->escape($_REQUEST['currency_name']));
  2073. }
  2074. } else {
  2075. $request = 1 / $rates[$this->default_currency];
  2076. }
  2077. } else {
  2078. if ($_REQUEST['currency_name'] != 'EUR') {
  2079. if ($rates[$_REQUEST['currency_name']] < 1) {
  2080. $request = 1 / $rates[$_REQUEST['currency_name']];
  2081. } else {
  2082. $request = $rates[$_REQUEST['currency_name']];
  2083. }
  2084. } else {
  2085. $request = 1;
  2086. }
  2087. }
  2088. } else {
  2089. $request = sprintf(__("no data for %s", 'woocommerce-currency-switcher'), $this->escape($_REQUEST['currency_name']));
  2090. }
  2091.  
  2092. //***
  2093.  
  2094. if (!$request) {
  2095. $request = sprintf(__("no data for %s", 'woocommerce-currency-switcher'), $this->escape($_REQUEST['currency_name']));
  2096. }
  2097.  
  2098.  
  2099. break;
  2100.  
  2101. case 'rf':
  2102. //http://www.cbr.ru/scripts/XML_daily_eng.asp?date_req=21/08/2015
  2103. $xml_url = 'http://www.cbr.ru/scripts/XML_daily_eng.asp?date_req='; //21/08/2015
  2104. $date = date('d/m/Y');
  2105. $xml_url .= $date;
  2106. if (function_exists('curl_init') AND $woocs_use_curl) {
  2107. $res = $this->file_get_contents_curl($xml_url);
  2108. } else {
  2109. $res = file_get_contents($xml_url);
  2110. }
  2111. //***
  2112. $xml = simplexml_load_string($res) or die("Error: Cannot create object");
  2113. $xml = $this->object2array($xml);
  2114. $rates = array();
  2115. $nominal = array();
  2116. //***
  2117. if (isset($xml['Valute'])) {
  2118. if (!empty($xml['Valute'])) {
  2119. foreach ($xml['Valute'] as $value) {
  2120. $rates[$value['CharCode']] = floatval(str_replace(',', '.', $value['Value']));
  2121. $nominal[$value['CharCode']] = $value['Nominal'];
  2122. }
  2123. }
  2124. }
  2125. //***
  2126. if (!empty($rates)) {
  2127. if ($this->default_currency != 'RUB') {
  2128. if ($_REQUEST['currency_name'] != 'RUB') {
  2129. if (isset($_REQUEST['currency_name'])) {
  2130. $request = $nominal[$this->escape($_REQUEST['currency_name'])] * floatval($rates[$this->default_currency] / $rates[$this->escape($_REQUEST['currency_name'])] / $nominal[$this->escape($this->default_currency)]);
  2131. } else {
  2132. $request = sprintf(__("no data for %s", 'woocommerce-currency-switcher'), $this->escape($_REQUEST['currency_name']));
  2133. }
  2134. } else {
  2135. if ($nominal[$this->default_currency] == 10) {
  2136. $request = (1 / (1 / $rates[$this->default_currency])) / $nominal[$this->default_currency];
  2137. } else {
  2138. $request = 1 / (1 / $rates[$this->default_currency]);
  2139. }
  2140. }
  2141. } else {
  2142. if ($_REQUEST['currency_name'] != 'RUB') {
  2143. $request = $nominal[$this->escape($_REQUEST['currency_name'])] / $rates[$_REQUEST['currency_name']];
  2144. } else {
  2145. $request = 1;
  2146. }
  2147. }
  2148. } else {
  2149. $request = sprintf(__("no data for %s", 'woocommerce-currency-switcher'), $this->escape($_REQUEST['currency_name']));
  2150. }
  2151.  
  2152. //***
  2153.  
  2154. if (!$request) {
  2155. $request = sprintf(__("no data for %s", 'woocommerce-currency-switcher'), $this->escape($_REQUEST['currency_name']));
  2156. }
  2157.  
  2158. break;
  2159.  
  2160. default:
  2161. break;
  2162. }
  2163.  
  2164.  
  2165. //***
  2166. if ($is_ajax) {
  2167. echo $request;
  2168. exit;
  2169. } else {
  2170. return $request;
  2171. }
  2172. }
  2173.  
  2174. private function object2array($object) {
  2175. return @json_decode(@json_encode($object), 1);
  2176. }
  2177.  
  2178. //ajax
  2179. public function save_etalon() {
  2180. if (!is_ajax() OR ! current_user_can('manage_options')) {
  2181. //we need it just only for ajax update
  2182. return "";
  2183. }
  2184. //$this->make_rates_auto_update(true);
  2185. $currencies = $this->get_currencies();
  2186. $currency_name = $this->escape($_REQUEST['currency_name']);
  2187. foreach ($currencies as $key => $currency) {
  2188. if ($currency['name'] == $currency_name) {
  2189. $currencies[$key]['is_etalon'] = 1;
  2190. } else {
  2191. $currencies[$key]['is_etalon'] = 0;
  2192. }
  2193. }
  2194. update_option('woocs', $currencies);
  2195. //+++ get curr updated values back
  2196. $request = array();
  2197. $this->default_currency = strtoupper($this->escape($_REQUEST['currency_name']));
  2198. $_REQUEST['no_ajax'] = TRUE;
  2199. foreach ($currencies as $key => $currency) {
  2200. if ($currency_name != $currency['name']) {
  2201. $_REQUEST['currency_name'] = $currency['name'];
  2202. $request[$key] = $this->get_rate();
  2203. } else {
  2204. $request[$key] = 1;
  2205. }
  2206. }
  2207.  
  2208. echo json_encode($request);
  2209. exit;
  2210. }
  2211.  
  2212. //order data registration
  2213. public function woocommerce_thankyou_order_id($order_id) {
  2214. $currencies = $this->get_currencies();
  2215. //+++
  2216. //$rate = get_post_meta($order_id, '_woocs_order_rate', true);
  2217. //if (intval($rate) === 0)
  2218. {
  2219. //condition (!$rate) is lock of chaning order currency looking it using link like
  2220. //http://xxxxx.currency-switcher.com/checkout/order-received/1003/?key=wc_order_55a52c81ad4ef
  2221. //this needs if back in paypal process, but looks like it is more dangerous, so commented
  2222. //update_post_meta($order_id, '_order_currency', $this->current_currency);
  2223. }
  2224. //+++
  2225. update_post_meta($order_id, '_woocs_order_rate', $currencies[$this->current_currency]['rate']);
  2226. wc_add_order_item_meta($order_id, '_woocs_order_rate', $currencies[$this->current_currency]['rate'], true);
  2227.  
  2228. update_post_meta($order_id, '_woocs_order_base_currency', $this->default_currency);
  2229. wc_add_order_item_meta($order_id, '_woocs_order_base_currency', $this->default_currency, true);
  2230.  
  2231. update_post_meta($order_id, '_woocs_order_currency_changed_mannualy', 0);
  2232. wc_add_order_item_meta($order_id, '_woocs_order_currency_changed_mannualy', 0, true);
  2233.  
  2234. return $order_id;
  2235. }
  2236.  
  2237. public function woocommerce_cart_totals_order_total_html($output) {
  2238. //if ($this->current_currency == $this->default_currency)
  2239. {
  2240. return $output;
  2241. }
  2242. //experimental feature. Do not use it.
  2243. //***
  2244. $value = "&nbsp;(";
  2245. //***
  2246. $currencies = $this->get_currencies();
  2247. $amount = WC()->cart->total / $currencies[$this->current_currency]['rate'];
  2248. //***
  2249. $cc = $this->current_currency;
  2250. $this->current_currency = $this->default_currency;
  2251. $value .= __('Total in basic currency: ', 'woocommerce-currency-switcher') . $this->wc_price($amount, false, array('currency' => $this->default_currency));
  2252. $this->current_currency = $cc;
  2253. $value .= ")";
  2254. return $output . $value;
  2255. }
  2256.  
  2257. public function wc_price_args($default_args) {
  2258. if (in_array($this->current_currency, $this->no_cents)) {
  2259. $default_args['decimals'] = 0;
  2260. }
  2261. return $default_args;
  2262. }
  2263.  
  2264. //***************************** email actions
  2265.  
  2266. public function woocommerce_email_actions($email_actions) {
  2267. $_REQUEST['woocs_order_emails_is_sending'] = 1;
  2268. if (isset($_REQUEST['woocs_in_order_currency'])) {
  2269. $this->current_currency = $_REQUEST['woocs_in_order_currency'];
  2270. //$this->default_currency = $_REQUEST['woocs_in_order_currency'];
  2271. } else {
  2272. global $post;
  2273. if (is_object($post) AND $post->post_type == 'shop_order') {
  2274. //processing button pressed in: wp-admin/edit.php?post_type=shop_order
  2275. $currency = get_post_meta($post->ID, '_order_currency', true);
  2276. if (!empty($currency)) {
  2277. $_REQUEST['woocs_in_order_currency'] = $currency;
  2278. $this->current_currency = $currency;
  2279. }
  2280. } else {
  2281. //processing button pressed in: wp-admin/post.php?post=1170&action=edit - inside of order by drop-down on the left
  2282. //print_r($_POST);
  2283. if (isset($_POST['order_status']) AND isset($_POST['post_ID'])) {
  2284. $currency = get_post_meta((int) $_POST['post_ID'], '_order_currency', true);
  2285. //echo $currency;exit;
  2286. if (!empty($currency)) {
  2287. $_REQUEST['woocs_in_order_currency'] = $currency;
  2288. $this->current_currency = $currency;
  2289. }
  2290. }
  2291. }
  2292. }
  2293.  
  2294.  
  2295.  
  2296. return $email_actions;
  2297. }
  2298.  
  2299. public function woocommerce_before_resend_order_emails($order) {
  2300. $currency = get_post_meta($order->id, '_order_currency', true);
  2301. if (!empty($currency)) {
  2302. $_REQUEST['woocs_in_order_currency'] = $currency;
  2303. $this->current_currency = $currency;
  2304. $this->default_currency = $currency;
  2305. }
  2306. }
  2307.  
  2308. //when admin complete order
  2309. public function woocommerce_order_status_completed($order_id) {
  2310. if (get_option('woocs_is_multiple_allowed', 0)) {
  2311. $currency = get_post_meta($order_id, '_order_currency', true);
  2312. if (!empty($currency)) {
  2313. $_REQUEST['woocs_in_order_currency'] = $currency;
  2314. $this->default_currency = $currency;
  2315. }
  2316. }
  2317. }
  2318.  
  2319. //wp-content\plugins\woocommerce\includes\class-wc-emails.php
  2320. //public static function init_transactional_emails()
  2321. //public static function send_transactional_email()
  2322. public function woocommerce_order_status_completed_notification($args) {
  2323. if (get_option('woocs_is_multiple_allowed', 0)) {
  2324. $order_id = $args;
  2325. $currency = get_post_meta($order_id, '_order_currency', true);
  2326. if (!empty($currency)) {
  2327. $_REQUEST['woocs_in_order_currency'] = $currency;
  2328. $this->default_currency = $currency;
  2329. $this->current_currency = $currency;
  2330. }
  2331. }
  2332. }
  2333.  
  2334. //********************************************************************************
  2335.  
  2336. public function wp_footer() {
  2337. //return; //return it for releases if you want
  2338. if (isset($_GET['currency'])) {
  2339. ?>
  2340. <script type="text/javascript">
  2341. try {
  2342. jQuery(function () {
  2343. try {
  2344. //https://wordpress.org/support/topic/wrong-cookie-leads-to-ajax-request-on-every-page/
  2345. jQuery.cookie('woocommerce_cart_hash', '', {path: '/'});
  2346. } catch (e) {
  2347. //***
  2348. }
  2349. });
  2350. } catch (e) {
  2351.  
  2352. }
  2353. </script>
  2354. <?php
  2355. }
  2356. }
  2357.  
  2358. //********************************************************************************
  2359.  
  2360. public function render_html($pagepath, $data = array()) {
  2361. @extract($data);
  2362. ob_start();
  2363. include($pagepath);
  2364. return ob_get_clean();
  2365. }
  2366.  
  2367. public function get_sign_rate($atts) {
  2368. $sign = strtoupper($atts['sign']);
  2369. $currencies = $this->get_currencies();
  2370. $rate = 0;
  2371. if (isset($currencies[$sign])) {
  2372. $rate = $currencies[$sign]['rate'];
  2373. }
  2374.  
  2375. return $rate;
  2376. }
  2377.  
  2378. //for hook woocommerce_paypal_args
  2379. function apply_conversion($paypal_args) {
  2380. if (in_array($this->current_currency, $this->no_cents)) {
  2381. $paypal_args['currency_code'] = $this->current_currency;
  2382. foreach ($paypal_args as $key => $value) {
  2383. if (strpos($key, 'amount_') !== false) {
  2384. $paypal_args[$key] = number_format($value, 0, $this->decimal_sep, '');
  2385. } else {
  2386. if (strpos($key, 'tax_cart') !== false) {
  2387. $paypal_args[$key] = number_format($value, 0, $this->decimal_sep, '');
  2388. }
  2389. }
  2390. }
  2391. }
  2392.  
  2393. return $paypal_args;
  2394. }
  2395.  
  2396. public function woocommerce_price_html($price_html, $product) {
  2397.  
  2398. static $customer_price_format = -1;
  2399. if ($customer_price_format === -1) {
  2400. $customer_price_format = get_option('woocs_customer_price_format', '__PRICE__');
  2401. }
  2402.  
  2403. if (empty($customer_price_format)) {
  2404. $customer_price_format = '__PRICE__';
  2405. }
  2406.  
  2407. //***
  2408. $currencies = $this->get_currencies();
  2409.  
  2410.  
  2411. if (version_compare(WOOCOMMERCE_VERSION, '2.7', '>=')) {
  2412. $product_id = $product->get_id();
  2413. } else {
  2414. $product_id = $product->id;
  2415. }
  2416.  
  2417. //+++
  2418. if (!empty($customer_price_format)) {
  2419. $txt = '<span class="woocs_price_code" data-product-id="' . $product_id . '">' . $customer_price_format . '</span>';
  2420. $txt = str_replace('__PRICE__', $price_html, $txt);
  2421. $price_html = str_replace('__CODE__', $this->current_currency, $txt);
  2422. $price_html = apply_filters('woocs_price_html_tail', $price_html);
  2423. }
  2424.  
  2425.  
  2426. //hide cents on front as html element
  2427. if (!in_array($this->current_currency, $this->no_cents)) {
  2428. $sep = wc_get_price_decimal_separator();
  2429. $zeros = str_repeat('[0-9]', $this->get_currency_price_num_decimals($this->current_currency));
  2430. if ($currencies[$this->current_currency]['hide_cents'] == 1) {
  2431. $price_html = preg_replace("/\\{$sep}{$zeros}/", '', $price_html);
  2432. }
  2433. }
  2434.  
  2435.  
  2436.  
  2437. //add additional info in price html
  2438. if (get_option('woocs_price_info', 0) AND ! (is_admin() AND ! isset($_REQUEST['get_product_price_by_ajax'])) AND ! isset($_REQUEST['hide_woocs_price_info_list'])) {
  2439. $info = "<ul class='woocs_price_info_list'>";
  2440. $current_currency = $this->current_currency;
  2441. foreach ($currencies as $сurr) {
  2442. if ($сurr['name'] == $current_currency) {
  2443. continue;
  2444. }
  2445. $this->current_currency = $сurr['name'];
  2446.  
  2447. if (version_compare(WOOCOMMERCE_VERSION, '2.7', '>=')) {
  2448. $value = $product->get_price('edit') * $currencies[$сurr['name']]['rate'];
  2449. } else {
  2450. $value = $product->price * $currencies[$сurr['name']]['rate'];
  2451. }
  2452.  
  2453.  
  2454. $precision = $this->get_currency_price_num_decimals($сurr['name'], $this->price_num_decimals);
  2455. $value = number_format($value, $precision, $this->decimal_sep, '');
  2456.  
  2457. //***
  2458.  
  2459. $product_type = '';
  2460. if (version_compare(WOOCOMMERCE_VERSION, '2.7', '>=')) {
  2461. $product_type = $product->get_type();
  2462. } else {
  2463. $product_type = $product->product_type;
  2464. }
  2465.  
  2466. if ($product_type == 'variable') {
  2467.  
  2468. if (version_compare(WOOCOMMERCE_VERSION, '2.7', '>=')) {
  2469. $min_value = $product->get_variation_price('min', true) * $currencies[$сurr['name']]['rate'];
  2470. $max_value = $product->get_variation_price('max', true) * $currencies[$сurr['name']]['rate'];
  2471. } else {
  2472. $min_value = $product->min_variation_price * $currencies[$сurr['name']]['rate'];
  2473. $max_value = $product->max_variation_price * $currencies[$сurr['name']]['rate'];
  2474. }
  2475.  
  2476. //***
  2477. $min_max_values = $this->_get_min_max_variation_prices($product, $сurr['name']);
  2478. if (!empty($min_max_values)) {
  2479. //echo '<pre>' . print_r($min_max_values, true) . '</pre>';
  2480. $min_value = $min_max_values['min'] /* $currencies[$сurr['name']]['rate'] */;
  2481. $max_value = $min_max_values['max'] /* $currencies[$сurr['name']]['rate'] */;
  2482. }
  2483. //+++
  2484. $_REQUEST['woocs_wc_price_convert'] = FALSE;
  2485.  
  2486. $var_price = $this->wc_price($min_value, array('currency' => $сurr['name']), false, $product, $precision);
  2487. $var_price .= ' - ';
  2488. $var_price .= $this->wc_price($max_value, array('currency' => $сurr['name']), false, $product, $precision);
  2489.  
  2490. unset($_REQUEST['woocs_wc_price_convert']);
  2491. $info .= "<li><b>" . $сurr['name'] . "</b>: " . $var_price . "</li>";
  2492. } else {
  2493. //print_r($product);
  2494. $info .= "<li><span>" . $сurr['name'] . "</span>: " . $this->wc_price($value, false, array('currency' => $сurr['name']), $product, $precision) . "</li>";
  2495. }
  2496. }
  2497. $this->current_currency = $current_currency;
  2498. $info .= "</ul>";
  2499. $info = '<div class="woocs_price_info"><span class="woocs_price_info_icon"></span>' . $info . '</div>';
  2500. $price_html .= $info;
  2501. }
  2502.  
  2503.  
  2504. return $price_html;
  2505. }
  2506.  
  2507. public function woocommerce_coupon_get_discount_amount($discount, $discounting_amount, $cart_item, $single, $coupon) {
  2508.  
  2509. if (version_compare(WOOCOMMERCE_VERSION, '2.4', '>=') AND $this->is_multiple_allowed) {
  2510. if (is_object($coupon) AND method_exists($coupon, 'is_type')) {
  2511. if (!$coupon->is_type(array('percent_product', 'percent'))) {
  2512. $discount = $this->woocs_exchange_value(floatval($discount));
  2513. }
  2514. }
  2515. }
  2516.  
  2517. return $discount;
  2518. }
  2519.  
  2520. public function woocommerce_coupon_validate_minimum_amount($is, $coupon) {
  2521.  
  2522. if ($this->current_currency != $this->default_currency) {
  2523. $currencies = $this->get_currencies();
  2524. //convert amount into basic currency amount
  2525. $cart_amount = $this->back_convert(WC()->cart->get_displayed_subtotal(), $currencies[$this->current_currency]['rate']);
  2526. return $coupon->minimum_amount > $cart_amount;
  2527. }
  2528.  
  2529. return $is;
  2530. }
  2531.  
  2532. public function woocommerce_coupon_validate_maximum_amount($is, $coupon) {
  2533.  
  2534. if ($this->current_currency != $this->default_currency) {
  2535. $currencies = $this->get_currencies();
  2536. //convert amount into basic currency amount
  2537. $cart_amount = $this->back_convert(WC()->cart->get_displayed_subtotal(), $currencies[$this->current_currency]['rate']);
  2538. return $coupon->maximum_amount < $cart_amount;
  2539. }
  2540.  
  2541. return $is;
  2542. }
  2543.  
  2544. public function woocommerce_coupon_error($err, $err_code, $coupon) {
  2545. if ($this->current_currency != $this->default_currency) {
  2546. $currencies = $this->get_currencies();
  2547.  
  2548. switch ($err_code) {
  2549. case 112:
  2550. $amount = $coupon->maximum_amount * $currencies[$this->current_currency]['rate'];
  2551. $err = sprintf(__('The maximum spend for this coupon is %s.', 'woocommerce-currency-switcher'), wc_price($amount));
  2552. break;
  2553.  
  2554. case 108:
  2555. $amount = $coupon->minimum_amount * $currencies[$this->current_currency]['rate'];
  2556. $err = sprintf(__('The minimum spend for this coupon is %s.', 'woocommerce-currency-switcher'), wc_price($amount));
  2557. break;
  2558.  
  2559. default:
  2560. break;
  2561. }
  2562. }
  2563.  
  2564. return $err;
  2565. }
  2566.  
  2567. //wp filter for values which is in basic currency and no possibility do it automatically
  2568. public function woocs_exchange_value($value) {
  2569. $currencies = $this->get_currencies();
  2570. $value = $value * $currencies[$this->current_currency]['rate'];
  2571. $precision = $this->get_currency_price_num_decimals($this->current_currency, $this->price_num_decimals);
  2572. $value = number_format($value, $precision, $this->decimal_sep, '');
  2573. return $value;
  2574. }
  2575.  
  2576. //set it to default
  2577. public function reset_currency() {
  2578. $this->set_currency('');
  2579. }
  2580.  
  2581. public function set_currency($currency = '') {
  2582. if (empty($currency)) {
  2583. $currency = $this->default_currency;
  2584. }
  2585. $this->storage->set_val('woocs_current_currency', $currency);
  2586. $this->current_currency = $currency;
  2587. }
  2588.  
  2589. //compatibility for https://wordpress.org/plugins/woocommerce-pdf-invoices-packing-slips/stats/
  2590. //hook commented, wpo_wcpdf_process_template_order uses for this
  2591. public function wpo_wcpdf_order_number($order_id) {
  2592. //set order currency instead selected on the front
  2593. $currency = get_post_meta($order_id, '_order_currency', TRUE);
  2594. if (!empty($currency)) {
  2595. $this->current_currency = $currency;
  2596. }
  2597.  
  2598. return $order_id;
  2599. }
  2600.  
  2601. //https://wordpress.org/support/topic/multi-currency-on-invoices?replies=8
  2602. public function wpo_wcpdf_process_template_order($template_type, $order_id) {
  2603. global $wpo_wcpdf;
  2604.  
  2605. if (isset($wpo_wcpdf->export->order->id)) {
  2606. $order_id = (int) $wpo_wcpdf->export->order->id;
  2607. $currency = get_post_meta($order_id, '_order_currency', TRUE);
  2608. if (!empty($currency)) {
  2609. $this->current_currency = $currency;
  2610. }
  2611. }
  2612. }
  2613.  
  2614. //***
  2615.  
  2616. public function woocommerce_get_order_currency($order_currency, $order) {
  2617.  
  2618. if (!is_ajax() AND ! is_admin() AND is_object($order)) {
  2619.  
  2620. $currency = get_post_meta($order->id, '_order_currency', TRUE);
  2621. if (!empty($currency)) {
  2622. $this->current_currency = $currency;
  2623. }
  2624. }
  2625.  
  2626. return $order_currency;
  2627. }
  2628.  
  2629. public function woocommerce_view_order($order_id) {
  2630.  
  2631. if (!is_ajax() AND ! is_admin()) {
  2632. $currency = get_post_meta($order_id, '_order_currency', TRUE);
  2633. if (!empty($currency)) {
  2634. $this->current_currency = $currency;
  2635. }
  2636. }
  2637.  
  2638. return $order_id;
  2639. }
  2640.  
  2641. public function woocommerce_package_rates($rates) {
  2642. $currencies = $this->get_currencies();
  2643.  
  2644. //***
  2645. if ($this->is_multiple_allowed) {
  2646. if ($this->current_currency != $this->default_currency) {
  2647. $currencies = $this->get_currencies();
  2648. foreach ($rates as $rate) {
  2649. $value = $rate->cost * $currencies[$this->current_currency]['rate'];
  2650. $precision = $this->get_currency_price_num_decimals($this->current_currency, $this->price_num_decimals);
  2651. $rate->cost = number_format(floatval($value), $precision, $this->decimal_sep, '');
  2652. //VAT values for another currency in the shipping
  2653. //https://wordpress.org/support/topic/vat-values-are-not-switched-to-another-currency-for-shipping
  2654. if (isset($rate->taxes)) {
  2655. $taxes = $rate->taxes;
  2656. if (!empty($taxes)) {
  2657. foreach ($taxes as $order => $tax) {
  2658. $value_tax = $tax * $currencies[$this->current_currency]['rate'];
  2659. $rate->taxes[$order] = number_format(floatval($value_tax), $precision, $this->decimal_sep, '');
  2660. }
  2661. }
  2662. }
  2663. }
  2664. }
  2665. }
  2666.  
  2667. return $rates;
  2668. }
  2669.  
  2670. public function wcml_raw_price_amount($value) {
  2671. return $this->woocs_exchange_value($value);
  2672. }
  2673.  
  2674. //ajax
  2675. public function woocs_convert_currency() {
  2676. $currencies = $this->get_currencies();
  2677. $v = $currencies[$_REQUEST['to']]['rate'] / $currencies[$_REQUEST['from']]['rate'];
  2678. if (in_array($_REQUEST['to'], $this->no_cents)) {
  2679. $_REQUEST['precision'] = 0;
  2680. }
  2681. $value = number_format($v * $_REQUEST['amount'], intval($_REQUEST['precision']), $this->decimal_sep, '');
  2682.  
  2683.  
  2684. wp_die($value);
  2685. }
  2686.  
  2687. //for refreshing mini-cart widget
  2688. public function woocommerce_before_mini_cart() {
  2689. $_REQUEST['woocs_woocommerce_before_mini_cart'] = 'mini_cart_refreshing';
  2690. WC()->cart->calculate_totals();
  2691. }
  2692.  
  2693. //for refreshing mini-cart widget
  2694. public function woocommerce_after_mini_cart() {
  2695. unset($_REQUEST['woocs_woocommerce_before_mini_cart']);
  2696. }
  2697.  
  2698. //ajax
  2699. public function woocs_rates_current_currency() {
  2700. wp_die(do_shortcode('[woocs_rates exclude="' . $this->escape($_REQUEST['exclude']) . '" precision="' . $this->escape($_REQUEST['precision']) . '" current_currency="' . $this->escape($_REQUEST['current_currency']) . '"]'));
  2701. }
  2702.  
  2703. //log test data while makes debbuging
  2704. public function log($string) {
  2705. $handle = fopen(WOOCS_PATH . 'log.txt', 'a+');
  2706. $string .= PHP_EOL;
  2707. fwrite($handle, $string);
  2708. fclose($handle);
  2709. }
  2710.  
  2711. public function wc_price($price, $convert = true, $args = array(), $product = NULL, $decimals = -1) {
  2712. if (!isset($_REQUEST['woocs_wc_price_convert'])) {
  2713. $_REQUEST['woocs_wc_price_convert'] = true;
  2714. }
  2715. extract(apply_filters('wc_price_args', wp_parse_args($args, array(
  2716. 'ex_tax_label' => false,
  2717. 'currency' => '',
  2718. 'decimal_separator' => $this->decimal_sep,
  2719. 'thousand_separator' => $this->thousands_sep,
  2720. 'decimals' => $decimals,
  2721. 'price_format' => $this->woocommerce_price_format()
  2722. ))));
  2723.  
  2724. if ($decimals < 0) {
  2725. $decimals = $this->get_currency_price_num_decimals($currency, $this->price_num_decimals);
  2726. }
  2727.  
  2728. //***
  2729. $currencies = $this->get_currencies();
  2730. if (isset($currencies[$currency])/* AND !isset($_REQUEST['woocs_show_custom_price']) */) {
  2731. if ($currencies[$currency]['hide_cents']) {
  2732. $decimals = 0;
  2733. }
  2734. }
  2735.  
  2736. //***
  2737. $negative = $price < 0;
  2738. $special_convert = false;
  2739. $is_price_custom = false;
  2740. try {
  2741. if ($product !== NULL AND is_object($product)) {
  2742. if (version_compare(WOOCOMMERCE_VERSION, '2.7', '>=')) {
  2743. $product_id = $product->get_id();
  2744. } else {
  2745. $product_id = $product->id;
  2746. }
  2747.  
  2748. //***
  2749.  
  2750. if ($this->is_multiple_allowed) {
  2751. if ($this->is_fixed_enabled) {
  2752. //$type = $this->fixed->get_price_type($product, $price);
  2753. $type = 'sale';
  2754.  
  2755. $is_empty = $this->fixed->is_empty($product_id, $currency, $type);
  2756. $is_exists = $this->fixed->is_exists($product_id, $currency, $type);
  2757.  
  2758. if ($type == 'sale' AND $is_empty) {
  2759. $type = 'regular';
  2760. $is_exists = $this->fixed->is_exists($product_id, $currency, $type);
  2761. $is_empty = $this->fixed->is_empty($product_id, $currency, $type);
  2762. }
  2763.  
  2764. if ($is_exists AND ! $is_empty) {
  2765. $special_convert = true;
  2766. $is_price_custom = true;
  2767. if (floatval($this->fixed->get_value($product_id, $currency, $type)) > 0) {
  2768. $price = floatval($this->fixed->get_value($product_id, $currency, $type));
  2769. }
  2770. }
  2771. }
  2772. }
  2773. }
  2774. } catch (Exception $e) {
  2775.  
  2776. }
  2777.  
  2778. //***
  2779.  
  2780. if ($this->is_geoip_manipulation AND ! $is_price_custom AND ! is_null($product)) {
  2781. $product_geo_price_data = $this->_get_product_geo_price($product, $price, 'sale', true);
  2782. $price = $product_geo_price_data[0];
  2783. $is_price_custom = true;
  2784. //$special_convert = $convert = true;
  2785.  
  2786. $product_type = 'simple';
  2787. if (version_compare(WOOCOMMERCE_VERSION, '2.7', '>=')) {
  2788. $product_type = $product->get_type();
  2789. } else {
  2790. $product_type = $product->product_type;
  2791. }
  2792.  
  2793. if ($product_type == 'variable') {
  2794. if ($product_geo_price_data[1]) {
  2795. $is_price_custom = false;
  2796. }
  2797. } else {
  2798. if ($product_geo_price_data[1]) {
  2799. $price = $this->raw_woocommerce_price(floatval($negative ? $price * -1 : $price));
  2800. }
  2801. }
  2802. }
  2803.  
  2804. //***
  2805.  
  2806. if ($convert AND $_REQUEST['woocs_wc_price_convert'] AND ! $is_price_custom) {
  2807. $price = $this->raw_woocommerce_price(floatval($negative ? $price * -1 : $price));
  2808. }
  2809.  
  2810. //***
  2811.  
  2812. $price = apply_filters('formatted_woocommerce_price', number_format($price, $decimals, $decimal_separator, $thousand_separator), $price, $decimals, $decimal_separator, $thousand_separator);
  2813.  
  2814.  
  2815. if (apply_filters('woocommerce_price_trim_zeros', false) AND $decimals > 0) {
  2816. $price = wc_trim_zeros($price);
  2817. }
  2818.  
  2819. $formatted_price = ( $negative ? '-' : '' ) . sprintf($price_format, get_woocommerce_currency_symbol($currency), $price);
  2820. $return = '<span class="woocs_amount">' . $formatted_price . '</span>';
  2821.  
  2822. if ($ex_tax_label && wc_tax_enabled()) {
  2823. $return .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
  2824. }
  2825.  
  2826. //***
  2827.  
  2828. return apply_filters('wc_price', $return, $price, $args);
  2829. }
  2830.  
  2831. public function woocommerce_available_variation($variation_data, $product, $variation) {
  2832.  
  2833. $_REQUEST['woocs_woocommerce_available_variation_is'] = TRUE;
  2834. add_filter('raw_woocommerce_price', array($this, 'raw_woocommerce_price'), 9999);
  2835. $variation = wc_get_product($variation->get_id());
  2836.  
  2837.  
  2838. // See if prices should be shown for each variation after selection.
  2839. $show_variation_price = apply_filters('woocommerce_show_variation_price', $variation->get_price() === "" || $product->get_variation_sale_price('min') !== $product->get_variation_sale_price('max') || $product->get_variation_regular_price('min') !== $product->get_variation_regular_price('max'), $product, $variation);
  2840. //$variation->set_prop('price', 109);
  2841. //$variation->set_prop('regular_price', 110);
  2842. //$variation->set_prop('sale_price', 109);
  2843. $_REQUEST['hide_woocs_price_info_list'] = true;
  2844. $variation_data = apply_filters('woocs_woocommerce_available_variation', array_merge($variation->get_data(), array(
  2845. 'attributes' => $variation->get_variation_attributes(),
  2846. 'image' => wc_get_product_attachment_props($variation->get_image_id()),
  2847. 'weight_html' => wc_format_weight($variation->get_weight()),
  2848. 'dimensions_html' => wc_format_dimensions($variation->get_dimensions(false)),
  2849. 'price_html' => $show_variation_price ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
  2850. 'availability_html' => wc_get_stock_html($variation),
  2851. 'variation_id' => $variation->get_id(),
  2852. 'variation_is_visible' => $variation->variation_is_visible(),
  2853. 'variation_is_active' => $variation->variation_is_active(),
  2854. 'is_purchasable' => $variation->is_purchasable(),
  2855. 'display_price' => wc_get_price_to_display($variation),
  2856. 'display_regular_price' => wc_get_price_to_display($variation, array('price' => $variation->get_regular_price())),
  2857. 'dimensions' => wc_format_dimensions($variation->get_dimensions(false)),
  2858. 'min_qty' => $variation->get_min_purchase_quantity(),
  2859. 'max_qty' => 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : '',
  2860. 'backorders_allowed' => $variation->backorders_allowed(),
  2861. 'is_in_stock' => $variation->is_in_stock(),
  2862. 'is_downloadable' => $variation->is_downloadable(),
  2863. 'is_virtual' => $variation->is_virtual(),
  2864. 'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',
  2865. 'variation_description' => $variation->get_description(),
  2866. )), $product, $variation);
  2867.  
  2868. unset($_REQUEST['hide_woocs_price_info_list']);
  2869. unset($_REQUEST['woocs_woocommerce_available_variation_is']);
  2870. remove_filter('raw_woocommerce_price', array($this, 'raw_woocommerce_price'), 9999);
  2871.  
  2872. /*
  2873. echo '<pre>';
  2874. print_r($variation_data);
  2875. echo '</pre>';
  2876. */
  2877. //return $variation_data;
  2878. return apply_filters('woocs_woocommerce_available_variation', $variation_data, $product, $variation);
  2879. }
  2880.  
  2881. //woo hook
  2882. public function woocommerce_product_is_on_sale($value, $product) {
  2883. //$product->get_sale_price() !== $product->get_regular_price() && $product->get_sale_price() === $product->get_price()
  2884. $is_sale = false;
  2885. $sale_price = $product->sale_price;
  2886. $regular_price = $product->regular_price;
  2887. $price = $product->price;
  2888.  
  2889. //***
  2890. //https://www.skyverge.com/blog/get-a-list-of-woocommerce-sale-products/
  2891. if ($product->product_type == 'variable') {
  2892. /*
  2893. $_REQUEST['woocs_block_price_hook'] = 1;
  2894. remove_all_filters('woocommerce_product_is_on_sale');
  2895. if ($product->is_on_sale())
  2896. {
  2897. $is_sale = true;
  2898. }
  2899. add_filter('woocommerce_product_is_on_sale', array($this, 'woocommerce_product_is_on_sale'), 9999, 2);
  2900. unset($_REQUEST['woocs_block_price_hook']);
  2901. */
  2902. } else {
  2903. if ($sale_price !== $regular_price AND ( $price === $sale_price)) {
  2904. $is_sale = true;
  2905. }
  2906. }
  2907.  
  2908.  
  2909. return $is_sale;
  2910. }
  2911.  
  2912. //woo hook
  2913. //wp-content\plugins\woocommerce\includes\shipping\free-shipping\class-wc-shipping-free-shipping.php #192
  2914. public function woocommerce_shipping_free_shipping_is_available($is_available, $package) {
  2915. global $wpdb;
  2916. //$wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", "%_transient_%"));
  2917. //print_r(WC()->session);
  2918. //unset(WC()->session->shipping_for_package_0);
  2919. //unset(WC()->session->shipping_method_counts);
  2920. //***
  2921. global $woocommerce;
  2922. $currencies = $this->get_currencies();
  2923.  
  2924. if (version_compare(WOOCOMMERCE_VERSION, '2.6.0', '<')) {
  2925. $free_shipping_settings = get_option('woocommerce_free_shipping_settings');
  2926. $min_amount = $free_shipping_settings['min_amount'];
  2927.  
  2928. if ($min_amount > 0) {
  2929. if ($this->current_currency != $this->default_currency) {
  2930. $min_amount = $this->woocs_exchange_value($min_amount);
  2931. if ($min_amount <= $package['contents_cost']) {
  2932. $is_available = true;
  2933. } else {
  2934. $is_available = false;
  2935. }
  2936. }
  2937. }
  2938. } else {
  2939.  
  2940. //from woo 2.6.0 control of free shipping is doing in woocommerce_package_rates
  2941. //wp-content\plugins\woocommerce\includes\class-wc-shipping.php -> calculate_shipping_for_package
  2942. //https://wordpress.org/support/topic/shop-for-x-more-to-get-free-shipping-in-woocommerce-26x
  2943. $wc_shipping = WC_Shipping::instance();
  2944. if ($wc_shipping->enabled) {
  2945. if (!empty($wc_shipping->shipping_methods)) {
  2946. foreach ($wc_shipping->shipping_methods as $key => $o) {
  2947. if (get_class($o) == 'WC_Shipping_Free_Shipping') {
  2948. $free_shipping_id = (int) $o->instance_id;
  2949. //$_REQUEST['free_shipping_id'] = $free_shipping_id;
  2950. //***
  2951. $free_shipping_settings = get_option('woocommerce_free_shipping_' . $free_shipping_id . '_settings');
  2952. $allows_array = array('min_amount', 'either', 'both');
  2953. if (in_array($free_shipping_settings['requires'], $allows_array)) {
  2954. $min_amount = $free_shipping_settings['min_amount'];
  2955.  
  2956. //$amount = floatval(preg_replace('#[^\d.]#', '', $woocommerce->cart->get_cart_total()));
  2957.  
  2958. /*
  2959. if (isset(WC()->session->subtotal_ex_tax)) {
  2960. $amount = WC()->session->subtotal_ex_tax;
  2961. } else {
  2962. $amount = WC()->session->subtotal;
  2963. }
  2964. */
  2965.  
  2966. $amount = WC()->session->subtotal;
  2967.  
  2968.  
  2969. if ($this->is_multiple_allowed) {
  2970. if ($this->current_currency != $this->default_currency) {
  2971. $amount = (float) $this->back_convert($amount, $currencies[$this->current_currency]['rate']);
  2972. //$amount = $amount + $amount * 0.001; //correction because of cents
  2973. }
  2974. }
  2975.  
  2976. //die($min_amount . '+++>' . $amount . '+++' . $this->current_currency . '+++' . $this->default_currency);
  2977.  
  2978. if ($amount >= $min_amount) {
  2979. $is_available = true;
  2980. } else {
  2981. $is_available = false;
  2982. }
  2983.  
  2984. //***
  2985. $free_shipping_coupon = false;
  2986. if (!empty($woocommerce->cart->applied_coupons)) {
  2987. $coupon = new WC_Coupon($woocommerce->cart->applied_coupons[0]);
  2988. $free_shipping_coupon_val = get_post_meta($coupon->id, 'free_shipping', true);
  2989. if ($free_shipping_coupon_val == 'yes') {
  2990. //in in coupon enabled 'Allow free shipping' checkbox
  2991. $free_shipping_coupon = true;
  2992. }
  2993. }
  2994.  
  2995. if ($free_shipping_settings['requires'] == 'both') {
  2996. if ($free_shipping_coupon AND $amount >= $min_amount) {
  2997. $is_available = true;
  2998. } else {
  2999. $is_available = false;
  3000. }
  3001. }
  3002.  
  3003. //***
  3004.  
  3005. if ($free_shipping_settings['requires'] == 'either') {
  3006. if ($free_shipping_coupon) {
  3007. $is_available = true;
  3008. }
  3009.  
  3010. if ($amount >= $min_amount) {
  3011. $is_available = true;
  3012. }
  3013. }
  3014. }
  3015. //$is_available=true;
  3016. //***
  3017. break;
  3018. }
  3019. }
  3020. }
  3021. }
  3022. }
  3023.  
  3024. //***
  3025.  
  3026. return $is_available;
  3027. }
  3028.  
  3029. //ajax
  3030. //for price redrawing on front if site using cache plugin functionality
  3031. public function woocs_get_products_price_html() {
  3032. $result = array();
  3033. if (isset($_REQUEST['products_ids'])) {
  3034. //***
  3035. $this->init_geo_currency();
  3036. //***
  3037. $_REQUEST['get_product_price_by_ajax'] = 1;
  3038.  
  3039. //add_action('woocommerce_price_html', array($this, 'woocommerce_price_html'), 1, 2);
  3040.  
  3041. $products_ids = $_REQUEST['products_ids'];
  3042. //***
  3043. if (!empty($products_ids) AND is_array($products_ids)) {
  3044. foreach ($products_ids as $p_id) {
  3045. $product = wc_get_product($p_id);
  3046. if (is_object($product)) {
  3047. $result[$p_id] = $product->get_price_html();
  3048. }
  3049. }
  3050. }
  3051. }
  3052. //***
  3053. $data = array();
  3054. $data['ids'] = $result;
  3055. $data['current_currency'] = $this->current_currency;
  3056. wp_die(json_encode($data));
  3057. }
  3058.  
  3059. //count amount in basic currency from any currency
  3060. public function back_convert($amount, $rate, $precision = 4) {
  3061. return number_format((1 / $rate) * $amount, $precision, '.', '');
  3062. }
  3063.  
  3064. //recalculation order to basic currency data if order is in any another currency
  3065. public function recalculate_order($order_id) {
  3066.  
  3067. $order_currency = get_post_meta($order_id, '_order_currency', true);
  3068. //lets avoid recalculation for order which is already in
  3069. if ($order_currency == $this->default_currency OR empty($order_currency)) {
  3070. return;
  3071. }
  3072. $decimals = $this->get_currency_price_num_decimals($order_currency, $this->price_num_decimals);
  3073.  
  3074. //***
  3075. $currencies = $this->get_currencies();
  3076. $_woocs_order_rate = get_post_meta($order_id, '_woocs_order_rate', true);
  3077. if (empty($_woocs_order_rate)) {
  3078. $_woocs_order_rate = $currencies[$order_currency]['rate'];
  3079. }
  3080. //***
  3081.  
  3082. update_post_meta($order_id, '_woocs_order_currency', $this->default_currency);
  3083. update_post_meta($order_id, '_order_currency', $this->default_currency);
  3084.  
  3085. update_post_meta($order_id, '_woocs_order_base_currency', $this->default_currency);
  3086. wc_update_order_item_meta($order_id, '_woocs_order_base_currency', $this->default_currency);
  3087.  
  3088. update_post_meta($order_id, '_woocs_order_rate', 1);
  3089. wc_update_order_item_meta($order_id, '_woocs_order_rate', 1);
  3090.  
  3091. update_post_meta($order_id, '_woocs_order_currency_changed_mannualy', time());
  3092. wc_add_order_item_meta($order_id, '_woocs_order_currency_changed_mannualy', time(), true);
  3093.  
  3094. //***
  3095.  
  3096. $_order_shipping = get_post_meta($order_id, '_order_shipping', true);
  3097. update_post_meta($order_id, '_order_shipping', $this->back_convert($_order_shipping, $_woocs_order_rate));
  3098.  
  3099. $_order_total = get_post_meta($order_id, '_order_total', true);
  3100. update_post_meta($order_id, '_order_total', $this->back_convert($_order_total, $_woocs_order_rate));
  3101.  
  3102. $_refund_amount = get_post_meta($order_id, '_refund_amount', true);
  3103. update_post_meta($order_id, '_refund_amount', $this->back_convert($_refund_amount, $_woocs_order_rate));
  3104.  
  3105. $_cart_discount_tax = get_post_meta($order_id, '_cart_discount_tax', true);
  3106. update_post_meta($order_id, '_cart_discount_tax', $this->back_convert($_cart_discount_tax, $_woocs_order_rate));
  3107.  
  3108. $_order_tax = get_post_meta($order_id, '_order_tax', true);
  3109. update_post_meta($order_id, '_order_tax', $this->back_convert($_order_tax, $_woocs_order_rate));
  3110.  
  3111. $_order_shipping_tax = get_post_meta($order_id, '_order_shipping_tax', true);
  3112. update_post_meta($order_id, '_order_shipping_tax', $this->back_convert($_order_shipping_tax, $_woocs_order_rate));
  3113.  
  3114. $_cart_discount = get_post_meta($order_id, '_cart_discount', true);
  3115. update_post_meta($order_id, '_cart_discount', $this->back_convert($_cart_discount, $_woocs_order_rate));
  3116.  
  3117. //***
  3118.  
  3119. global $wpdb;
  3120. $get_items_sql = $wpdb->prepare("SELECT order_item_id,order_item_type FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d ", $order_id);
  3121. $line_items = $wpdb->get_results($get_items_sql, ARRAY_N);
  3122. if (!empty($line_items) AND is_array($line_items)) {
  3123. foreach ($line_items as $v) {
  3124. $order_item_id = $v[0];
  3125. $order_item_type = $v[1];
  3126.  
  3127. switch ($order_item_type) {
  3128. case 'line_item':
  3129.  
  3130. $amount = wc_get_order_item_meta($order_item_id, '_line_subtotal', true);
  3131. wc_update_order_item_meta($order_item_id, '_line_subtotal', $this->back_convert($amount, $_woocs_order_rate, $decimals));
  3132.  
  3133. $amount = wc_get_order_item_meta($order_item_id, '_line_total', true);
  3134. wc_update_order_item_meta($order_item_id, '_line_total', $this->back_convert($amount, $_woocs_order_rate, $decimals));
  3135.  
  3136. $amount = wc_get_order_item_meta($order_item_id, '_line_subtotal_tax', true);
  3137. wc_update_order_item_meta($order_item_id, '_line_subtotal_tax', $this->back_convert($amount, $_woocs_order_rate, $decimals));
  3138.  
  3139. $amount = wc_get_order_item_meta($order_item_id, '_line_tax', true);
  3140. wc_update_order_item_meta($order_item_id, '_line_tax', $this->back_convert($amount, $_woocs_order_rate, $decimals));
  3141.  
  3142. $_line_tax_data = wc_get_order_item_meta($order_item_id, '_line_tax_data', true);
  3143. if (!empty($_line_tax_data) AND is_array($_line_tax_data)) {
  3144. foreach ($_line_tax_data as $key => $values) {
  3145. if (!empty($values)) {
  3146. if (is_array($values)) {
  3147. foreach ($values as $k => $value) {
  3148. if (is_numeric($value)) {
  3149. $_line_tax_data[$key][$k] = $this->back_convert($value, $_woocs_order_rate, $decimals);
  3150. }
  3151. }
  3152. } else {
  3153. if (is_numeric($values)) {
  3154. $_line_tax_data[$key] = $this->back_convert($values, $_woocs_order_rate, $decimals);
  3155. }
  3156. }
  3157. }
  3158. }
  3159. }
  3160.  
  3161. wc_update_order_item_meta($order_item_id, '_line_tax_data', $_line_tax_data);
  3162.  
  3163. break;
  3164.  
  3165. case 'shipping':
  3166. $amount = wc_get_order_item_meta($order_item_id, 'cost', true);
  3167. wc_update_order_item_meta($order_item_id, 'cost', $this->back_convert($amount, $_woocs_order_rate, $decimals));
  3168.  
  3169.  
  3170. $taxes = wc_get_order_item_meta($order_item_id, 'taxes', true);
  3171.  
  3172. if (!empty($taxes) AND is_array($taxes)) {
  3173. foreach ($taxes as $key => $values) {
  3174. if (!empty($values)) {
  3175. if (is_array($values)) {
  3176. foreach ($values as $k => $value) {
  3177. if (is_numeric($value)) {
  3178. $taxes[$key][$k] = $this->back_convert($value, $_woocs_order_rate, $decimals);
  3179. }
  3180. }
  3181. } else {
  3182. if (is_numeric($values)) {
  3183. $taxes[$key] = $this->back_convert($values, $_woocs_order_rate, $decimals);
  3184. }
  3185. }
  3186. }
  3187. }
  3188. }
  3189.  
  3190. wc_update_order_item_meta($order_item_id, 'taxes', $taxes);
  3191.  
  3192. break;
  3193.  
  3194. case 'tax':
  3195. $amount = wc_get_order_item_meta($order_item_id, 'tax_amount', true);
  3196. wc_update_order_item_meta($order_item_id, 'tax_amount', $this->back_convert($amount, $_woocs_order_rate, 3));
  3197.  
  3198. $amount = wc_get_order_item_meta($order_item_id, 'shipping_tax_amount', true);
  3199. wc_update_order_item_meta($order_item_id, 'shipping_tax_amount', $this->back_convert($amount, $_woocs_order_rate, $decimals));
  3200.  
  3201. break;
  3202.  
  3203. default:
  3204. break;
  3205. }
  3206. }
  3207. }
  3208.  
  3209. //***
  3210.  
  3211. $order = new WC_Order($order_id);
  3212. $refunds = $order->get_refunds();
  3213.  
  3214. if (!empty($refunds)) {
  3215. foreach ($refunds as $refund) {
  3216. $post_id = $refund->id;
  3217. $amount = get_post_meta($post_id, '_refund_amount', true);
  3218. update_post_meta($post_id, '_refund_amount', $this->back_convert($amount, $_woocs_order_rate, $decimals));
  3219. $amount = get_post_meta($post_id, '_order_total', true);
  3220. update_post_meta($post_id, '_order_total', $this->back_convert($amount, $_woocs_order_rate, $decimals));
  3221. update_post_meta($post_id, '_order_currency', $this->default_currency);
  3222. }
  3223. }
  3224. }
  3225.  
  3226. //ajax
  3227. public function woocs_recalculate_order_data() {
  3228. if (!current_user_can('manage_options')) {
  3229. return;
  3230. }
  3231.  
  3232. $this->recalculate_order((int) $_REQUEST['order_id']);
  3233. wp_die('done');
  3234. }
  3235.  
  3236. //***************** BEGIN ADDITIONAL INFO HTML ON THE CHECKOUT+CART ***************
  3237. //only attach some info in html
  3238. //wp-content\plugins\woocommerce\templates\cart\cart.php
  3239. public function woocommerce_cart_item_price($product_price, $cart_item, $cart_item_key) {
  3240. $user_currency = $this->get_currency_by_country($this->storage->get_val('woocs_user_country'));
  3241. $currencies = $this->get_currencies();
  3242.  
  3243. if ($user_currency != $this->current_currency AND ! empty($user_currency)) {
  3244. $tmp_curr_currency = $this->current_currency;
  3245. $this->set_currency($user_currency);
  3246.  
  3247.  
  3248. //***
  3249. $back_convert = true;
  3250. if ($user_currency == $this->default_currency) {
  3251. $back_convert = false;
  3252. }
  3253. if ($this->is_multiple_allowed) {
  3254. $back_convert = true;
  3255. }
  3256. if (!$this->is_multiple_allowed AND ( $user_currency !== $this->default_currency)) {
  3257. $back_convert = false;
  3258. }
  3259. //***
  3260.  
  3261.  
  3262. if ($back_convert) {
  3263. $cart_price = $this->back_convert($cart_item['line_total'], $currencies[$tmp_curr_currency]['rate']) / $cart_item['quantity'];
  3264. } else {
  3265. $cart_price = $cart_item['line_total'] / $cart_item['quantity'];
  3266. }
  3267.  
  3268.  
  3269. $wc_price = $this->wc_price($cart_price, true, array('decimals' => $this->get_currency_price_num_decimals($user_currency, $this->price_num_decimals)));
  3270. $product_price .= $this->get_cart_item_price_html($wc_price);
  3271.  
  3272.  
  3273. $this->set_currency($tmp_curr_currency);
  3274. }
  3275.  
  3276. return $product_price;
  3277. }
  3278.  
  3279. //wp-content\plugins\woocommerce\templates\cart\cart.php
  3280. public function woocommerce_cart_item_subtotal($product_subtotal, $cart_item, $cart_item_key) {
  3281. $user_currency = $this->get_currency_by_country($this->storage->get_val('woocs_user_country'));
  3282. $currencies = $this->get_currencies();
  3283.  
  3284. if ($user_currency != $this->current_currency AND ! empty($user_currency)) {
  3285. $tmp_curr_currency = $this->current_currency;
  3286. $this->set_currency($user_currency);
  3287.  
  3288. $cart_amount = $cart_item['line_subtotal'];
  3289.  
  3290. //***
  3291. $back_convert = true;
  3292. if ($user_currency == $this->default_currency) {
  3293. $back_convert = false;
  3294. }
  3295. if ($this->is_multiple_allowed) {
  3296. $back_convert = true;
  3297. }
  3298. if (!$this->is_multiple_allowed AND ( $user_currency !== $this->default_currency)) {
  3299. $back_convert = false;
  3300. }
  3301. //***
  3302.  
  3303. if ($back_convert) {
  3304. $cart_amount = $this->back_convert($cart_amount, $currencies[$tmp_curr_currency]['rate']);
  3305. }
  3306.  
  3307.  
  3308. $wc_price = $this->wc_price($cart_amount, true, array('decimals' => $this->get_currency_price_num_decimals($user_currency, $this->price_num_decimals)));
  3309. $product_subtotal .= $this->get_cart_item_price_html($wc_price);
  3310.  
  3311.  
  3312. $this->set_currency($tmp_curr_currency);
  3313. }
  3314.  
  3315. return $product_subtotal;
  3316. }
  3317.  
  3318. //wp-content\plugins\woocommerce\templates\cart\cart-totals.php
  3319. public function woocommerce_cart_subtotal($cart_subtotal, $compound, $woo) {
  3320. $user_currency = $this->get_currency_by_country($this->storage->get_val('woocs_user_country'));
  3321. $currencies = $this->get_currencies();
  3322.  
  3323. if ($user_currency != $this->current_currency AND ! empty($user_currency)) {
  3324.  
  3325. $amount = 0;
  3326. if ($compound) {
  3327. $amount = $woo->cart_contents_total + $woo->shipping_total + $woo->get_taxes_total(false, false);
  3328. // Otherwise we show cart items totals only (before discount)
  3329. } else {
  3330.  
  3331. // Display varies depending on settings
  3332. if ($woo->tax_display_cart == 'excl') {
  3333. $amount = $woo->subtotal_ex_tax;
  3334. } else {
  3335. $amount = $woo->subtotal;
  3336. }
  3337. }
  3338. //***
  3339.  
  3340.  
  3341. $tmp_curr_currency = $this->current_currency;
  3342. $this->set_currency($user_currency);
  3343.  
  3344. //***
  3345. $back_convert = true;
  3346. if ($user_currency == $this->default_currency) {
  3347. $back_convert = false;
  3348. }
  3349. if ($this->is_multiple_allowed) {
  3350. $back_convert = true;
  3351. }
  3352. if (!$this->is_multiple_allowed AND ( $user_currency !== $this->default_currency)) {
  3353. $back_convert = false;
  3354. }
  3355. //***
  3356.  
  3357. if ($back_convert) {
  3358. $amount = $this->back_convert($amount, $currencies[$tmp_curr_currency]['rate']);
  3359. }
  3360.  
  3361. $wc_price = $this->wc_price($amount, true, array('decimals' => $this->get_currency_price_num_decimals($user_currency, $this->price_num_decimals)));
  3362. $cart_subtotal .= $this->get_cart_item_price_html($wc_price);
  3363.  
  3364.  
  3365. $this->set_currency($tmp_curr_currency);
  3366. }
  3367.  
  3368. return $cart_subtotal;
  3369. }
  3370.  
  3371. //wp-content\plugins\woocommerce\includes\class-wc-cart.php
  3372. public function woocommerce_cart_total($html_value) {
  3373.  
  3374. $user_currency = $this->get_currency_by_country($this->storage->get_val('woocs_user_country'));
  3375. $currencies = $this->get_currencies();
  3376.  
  3377. //***
  3378. if ($user_currency != $this->current_currency AND ! empty($user_currency)) {
  3379.  
  3380. $tmp_curr_currency = $this->current_currency;
  3381. $this->set_currency($user_currency);
  3382. $total = WC()->cart->total;
  3383.  
  3384. //***
  3385. $back_convert = true;
  3386. if ($user_currency == $this->default_currency) {
  3387. $back_convert = false;
  3388. }
  3389. if ($this->is_multiple_allowed) {
  3390. $back_convert = true;
  3391. }
  3392. if (!$this->is_multiple_allowed AND ( $user_currency !== $this->default_currency)) {
  3393. $back_convert = false;
  3394. }
  3395. //***
  3396.  
  3397. if ($back_convert) {
  3398. $total = $this->back_convert($total, $currencies[$tmp_curr_currency]['rate']);
  3399. }
  3400.  
  3401. $wc_price = $this->wc_price($total, true, array('decimals' => $this->get_currency_price_num_decimals($user_currency, $this->price_num_decimals)));
  3402. $html_value .= $this->get_cart_item_price_html($wc_price);
  3403.  
  3404.  
  3405. $this->set_currency($tmp_curr_currency);
  3406. }
  3407.  
  3408. //*** comment this block, for jeroen shipping plugin only
  3409. /*
  3410. if ($this->is_multiple_allowed AND $this->current_currency != $this->default_currency)
  3411. {
  3412. $total = WC()->cart->total;
  3413. $currencies = $this->get_currencies();
  3414. if (!empty(WC()->cart->shipping_taxes))
  3415. {
  3416. //as it recounted twice - down it!
  3417. foreach (WC()->cart->shipping_taxes as $key => $value)
  3418. {
  3419. $total = $total - ($value / $currencies[$this->current_currency]['rate'] - $value);
  3420. }
  3421. }
  3422.  
  3423. WC()->cart->total = $total;
  3424. return wc_price($total);
  3425. }
  3426. */
  3427.  
  3428.  
  3429.  
  3430. return $html_value;
  3431. }
  3432.  
  3433. //wp-content\plugins\woocommerce\includes\class-wc-cart.php
  3434. public function woocommerce_cart_totals_taxes_total_html($html_value) {
  3435.  
  3436. $user_currency = $this->get_currency_by_country($this->storage->get_val('woocs_user_country'));
  3437. $currencies = $this->get_currencies();
  3438.  
  3439. if ($user_currency != $this->current_currency AND ! empty($user_currency)) {
  3440. $tmp_curr_currency = $this->current_currency;
  3441. $this->set_currency($user_currency);
  3442.  
  3443. $total = 0;
  3444. $compound = true;
  3445. foreach (WC()->cart->taxes as $key => $tax) {
  3446. if (!$compound && WC_Tax::is_compound($key))
  3447. continue;
  3448. $total += $tax;
  3449. }
  3450. foreach (WC()->cart->shipping_taxes as $key => $tax) {
  3451. if (!$compound && WC_Tax::is_compound($key))
  3452. continue;
  3453. $total += $tax;
  3454. }
  3455.  
  3456.  
  3457. //***
  3458. $back_convert = true;
  3459. if ($user_currency == $this->default_currency) {
  3460. $back_convert = false;
  3461. }
  3462. if ($this->is_multiple_allowed) {
  3463. $back_convert = true;
  3464. }
  3465. if (!$this->is_multiple_allowed AND ( $user_currency !== $this->default_currency)) {
  3466. $back_convert = false;
  3467. }
  3468. //***
  3469.  
  3470. if ($back_convert) {
  3471. $total = $this->back_convert($total, $currencies[$tmp_curr_currency]['rate']);
  3472. }
  3473.  
  3474. $wc_price = $this->wc_price($total, true, array('decimals' => $this->get_currency_price_num_decimals($user_currency, $this->price_num_decimals)));
  3475. $html_value .= $this->get_cart_item_price_html($wc_price);
  3476.  
  3477.  
  3478. $this->set_currency($tmp_curr_currency);
  3479. }
  3480.  
  3481.  
  3482. return $html_value;
  3483. }
  3484.  
  3485. public function woocommerce_cart_tax_totals($tax_totals, $woo) {
  3486. //$woo is WC_Cart
  3487. $user_currency = $this->get_currency_by_country($this->storage->get_val('woocs_user_country'));
  3488. $currencies = $this->get_currencies();
  3489.  
  3490. if ($user_currency != $this->current_currency AND ! empty($user_currency)) {
  3491. $tmp_curr_currency = $this->current_currency;
  3492. $this->set_currency($user_currency);
  3493.  
  3494. if (!empty($tax_totals)) {
  3495. foreach ($tax_totals as $key => $o) {
  3496.  
  3497. $amount = $o->amount;
  3498.  
  3499.  
  3500. //***
  3501. $back_convert = true;
  3502. if ($user_currency == $this->default_currency) {
  3503. $back_convert = false;
  3504. }
  3505. if ($this->is_multiple_allowed) {
  3506. $back_convert = true;
  3507. }
  3508. if (!$this->is_multiple_allowed AND ( $user_currency !== $this->default_currency)) {
  3509. $back_convert = false;
  3510. }
  3511. //***
  3512.  
  3513. if ($back_convert) {
  3514. $amount = $this->back_convert($amount, $currencies[$tmp_curr_currency]['rate']);
  3515. }
  3516.  
  3517. $wc_price = $this->wc_price($amount, true, array('decimals' => $this->get_currency_price_num_decimals($user_currency, $this->price_num_decimals)));
  3518. $o->formatted_amount .= $this->get_cart_item_price_html($wc_price);
  3519. }
  3520. }
  3521.  
  3522.  
  3523. $this->set_currency($tmp_curr_currency);
  3524. }
  3525.  
  3526.  
  3527.  
  3528. return $tax_totals;
  3529. }
  3530.  
  3531. //wp-content\plugins\woocommerce\includes\wc-cart-functions.php
  3532. public function woocommerce_cart_shipping_method_full_label($label, $method) {
  3533. //$woo is WC_Cart
  3534. if ($method->cost > 0) {
  3535. $user_currency = $this->get_currency_by_country($this->storage->get_val('woocs_user_country'));
  3536. $currencies = $this->get_currencies();
  3537.  
  3538. if ($user_currency != $this->current_currency AND ! empty($user_currency)) {
  3539. $tmp_curr_currency = $this->current_currency;
  3540. $this->set_currency($user_currency);
  3541.  
  3542. if (WC()->cart->tax_display_cart == 'excl') {
  3543. $amount = $method->cost;
  3544. } else {
  3545. $amount = $method->cost + $method->get_shipping_tax();
  3546. }
  3547.  
  3548.  
  3549. //***
  3550. $back_convert = true;
  3551. if ($user_currency == $this->default_currency) {
  3552. $back_convert = false;
  3553. }
  3554. if ($this->is_multiple_allowed) {
  3555. $back_convert = true;
  3556. }
  3557. if (!$this->is_multiple_allowed AND ( $user_currency !== $this->default_currency)) {
  3558. $back_convert = false;
  3559. }
  3560. //***
  3561.  
  3562. if ($back_convert) {
  3563. $amount = $this->back_convert($amount, $currencies[$tmp_curr_currency]['rate']);
  3564. }
  3565.  
  3566. $wc_price = $this->wc_price($amount, true, array('decimals' => $this->get_currency_price_num_decimals($user_currency, $this->price_num_decimals)));
  3567.  
  3568. $label .= $this->get_cart_item_price_html($wc_price);
  3569.  
  3570. $this->set_currency($tmp_curr_currency);
  3571. }
  3572. }
  3573.  
  3574. return $label;
  3575. }
  3576.  
  3577. private function get_cart_item_price_html($wc_price) {
  3578. $html = '<div class="woocs_cart_item_price">';
  3579. $html .= apply_filters('woocs_get_approximate_amount_text', sprintf(__('(Approx. %s)'), $wc_price), $wc_price);
  3580. $html .= '</div>';
  3581. return $html;
  3582. }
  3583.  
  3584. //***************** END ADDITIONAL INFO HTML ON THE CHECKOUT+CART ***************
  3585. //custom code for Woocommerce Advanced Shipping by http://jeroensormani.com/ in multiple mode
  3586. public function woocommerce_cart_get_taxes($taxes, $woo_cart) {
  3587. if ($this->is_multiple_allowed AND $this->current_currency != $this->default_currency) {
  3588. $currencies = $this->get_currencies();
  3589. if (!empty($woo_cart->shipping_taxes)) {
  3590. //as it recounted twice - down it!
  3591. foreach ($woo_cart->shipping_taxes as $key => $value) {
  3592. $woo_cart->shipping_taxes[$key] = $value * $currencies[$this->current_currency]['rate'];
  3593. }
  3594. }
  3595. // Merge
  3596. foreach (array_keys($woo_cart->taxes + $woo_cart->shipping_taxes) as $key) {
  3597. $taxes[$key] = ( isset($woo_cart->shipping_taxes[$key]) ? $woo_cart->shipping_taxes[$key] : 0 ) + ( isset($woo_cart->taxes[$key]) ? $woo_cart->taxes[$key] : 0 );
  3598. }
  3599. }
  3600. return $taxes;
  3601. }
  3602.  
  3603. //class-wc-cart.php -> public function calculate_totals()
  3604. public function woocommerce_after_calculate_totals($woo_cart) {
  3605. if ($this->is_multiple_allowed AND $this->current_currency != $this->default_currency AND is_ajax()) {
  3606. if (isset($_POST['billing_address_1'])) {
  3607. $currencies = $this->get_currencies();
  3608. if (!empty($woo_cart->shipping_taxes)) {
  3609. //as it recounted twice - down it!
  3610. foreach ($woo_cart->shipping_taxes as $key => $value) {
  3611. $woo_cart->shipping_taxes[$key] = $value * $currencies[$this->current_currency]['rate'];
  3612. }
  3613. }
  3614. // Merge
  3615. foreach (array_keys($woo_cart->taxes + $woo_cart->shipping_taxes) as $key) {
  3616. $woo_cart->taxes[$key] = ( isset($woo_cart->shipping_taxes[$key]) ? $woo_cart->shipping_taxes[$key] : 0 ) + ( isset($woo_cart->taxes[$key]) ? $woo_cart->taxes[$key] : 0 );
  3617. }
  3618.  
  3619.  
  3620. //***
  3621.  
  3622. $total = $woo_cart->total;
  3623. $currencies = $this->get_currencies();
  3624. if (!empty($woo_cart->shipping_taxes)) {
  3625. //as it recounted twice - down it!
  3626. foreach ($woo_cart->shipping_taxes as $key => $value) {
  3627. $total = $total - ($value / $currencies[$this->current_currency]['rate'] - $value);
  3628. }
  3629. }
  3630.  
  3631. $woo_cart->total = $total;
  3632. }
  3633. }
  3634. }
  3635.  
  3636. public function escape($value) {
  3637. return sanitize_text_field(esc_html($value));
  3638. }
  3639.  
  3640. public function wc_get_template($located, $template_name, $args, $template_path, $default_path) {
  3641. if (isset($args['order'])) {
  3642. if (is_object($args['order']) AND ! is_null($args['order'])) {
  3643. $order = $args['order'];
  3644. //$currency = get_post_meta($order->id, '_order_currency', true);
  3645. //$this->set_currency($currency);
  3646. //echo $order->get_currency();exit;
  3647. //echo $template_name;exit;
  3648. if (substr($template_name, 0, 6) === 'emails') {
  3649. if (method_exists($order, 'get_currency')) {
  3650. $this->set_currency($order->get_currency());
  3651. }
  3652. }
  3653. }
  3654. }
  3655.  
  3656. return $located;
  3657. }
  3658.  
  3659. }
  3660.  
  3661. //+++
  3662. if (isset($_GET['P3_NOCACHE'])) {
  3663. //stupid trick for that who believes in P3
  3664. return;
  3665. }
  3666. //+++
  3667. $WOOCS = new WOOCS();
  3668. $GLOBALS['WOOCS'] = $WOOCS;
  3669. add_action('init', array($WOOCS, 'init'), 1);
  3670.  
  3671. //includes/wc-core-functions.php
  3672. //includes/wc-formatting-functions.php
  3673. //includes/admin/post-types/meta-boxes/class-wc-meta-box-order-totals.php
  3674. //wp-content\plugins\woocommerce\includes\wc-formatting-functions.php
  3675. //wp-content\plugins\woocommerce\includes\wc-cart-functions.php
  3676. //wp-content\plugins\woocommerce\includes\wc-conditional-functions.php
  3677. //wp-content\plugins\woocommerce\includes\class-wc-cart.php
  3678. //wp-content\plugins\woocommerce\includes\abstracts\abstract-wc-product.php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement