Currency Switcher, by realmag777
Add provided code to functions.php and update Settings.
Calculation option “Use prices modified by other plugins” must be ON.
System option “Suppress other pricing plugins in frontend” must be OFF.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
<?php if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } /** * Plugin Name: WOOCS - WooCommerce Currency Switcher * Plugin URI: https://currency-switcher.com/ * Author: realmag777 * * Class WDP_WOOCS_Compatibility */ class WDP_WOOCS_Compatibility { const KEY = 'realmag777_woocs'; const WDP_VERSION = '2.2.4'; const TARGET_VERSION = '2.2.8.1'; /** * @var null|WOOCS */ protected $woocs = null; /** * @var array */ private $currencies = array(); public function is_required() { return class_exists( 'WOOCS' ); } public function admin_install() { } public function remove_admin_hooks() { } /** * @param WDP_Price_Display $price_display */ public function install( $price_display ) { if ( ! $this->load_requirements() ) { return; } add_action( 'wdp_rule_init', array( $this, 'set_rule_currency' ), 10, 1 ); add_action( 'woocommerce_before_calculate_totals', array( $this, 'kill_currency_hooks' ), 10, 0 ); add_action( 'woocommerce_after_calculate_totals', array( $this, 'activate_currency_hooks' ), 10, 0 ); } public function remove_hooks() { remove_filter( 'wdp_save_cart_item_keys', array( $this, 'wdp_save_cart_item_keys' ), 10 ); remove_filter( 'wdp_get_product_price', array( $this, 'wdp_get_product_price' ), 10 ); } public function load_requirements() { $this->woocs = isset( $GLOBALS['WOOCS'] ) ? $GLOBALS['WOOCS'] : null; if ( $this->woocs ) { $this->currencies = $this->woocs->get_currencies(); } return ! is_null( $this->woocs ) && ( $this->woocs instanceof WOOCS ); } public function activate_currency_hooks() { add_filter( 'woocommerce_product_get_price', array( $this->woocs, 'raw_woocommerce_price' ), 9999, 2 ); add_filter( 'woocommerce_product_variation_get_price', array( $this->woocs, 'raw_woocommerce_price' ), 9999, 2 ); } public function kill_currency_hooks() { remove_filter( 'woocommerce_product_get_price', array( $this->woocs, 'raw_woocommerce_price' ), 9999 ); remove_filter( 'woocommerce_product_variation_get_price', array( $this->woocs, 'raw_woocommerce_price' ), 9999 ); } /** * @param WDP_Rule $rule */ public function set_rule_currency( $rule ) { $currency = $this->woocs->current_currency; $default_currency = $this->woocs->default_currency; $rate = (float) $this->get_currency_prop( $currency, 'rate' ) / (float) $this->get_currency_prop( $default_currency, 'rate' ); $rule->set_currency( $currency, $rate ); } protected function is_woocs_exists() { return ! is_null( $this->woocs ) && ( $this->woocs instanceof WOOCS ); } protected function get_currency( $currency ) { $currency_data = null; if ( isset( $this->currencies[ $currency ] ) && ! is_null( $this->currencies[ $currency ] ) ) { $currency_data = $this->currencies[ $currency ]; } return $currency_data; } protected function get_currency_prop( $currency, $prop ) { $currency = $this->get_currency( $currency ); return ! is_null( $currency ) && isset( $currency[ $prop ] ) ? $currency[ $prop ] : null; } } $cmp = new WDP_WOOCS_Compatibility(); if ( $cmp ) { $cmp->install(null); } |