-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal-currency.php
More file actions
226 lines (197 loc) · 5.94 KB
/
Copy pathlocal-currency.php
File metadata and controls
226 lines (197 loc) · 5.94 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/**
* Plugin Name: WC Local Currency Display
* Plugin URI: https://example.com
* Description: Displays WooCommerce product prices in the visitor's local currency based on their IP address.
* Version: 1.0.0
* Author: Your Name
* Author URI: https://example.com
* Text Domain: wc-local-currency
* WC requires at least: 5.0
* WC tested up to: 8.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WC_Local_Currency_Display {
private static $instance = null;
private $api_url = 'https://api.exchangerate-api.com/v4/latest/';
private $target_currency = '';
private $base_currency = '';
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
add_action( 'plugins_loaded', [ $this, 'init' ] );
add_action( 'before_woocommerce_init', [ $this, 'declare_hpos_compatibility' ] );
}
/**
* Declare compatibility with High-Performance Order Storage (HPOS).
*/
/**
* Declare compatibility with High-Performance Order Storage (HPOS).
*
* HPOS is WooCommerce’s modern order-storage layer that moves orders from wp_posts into custom tables.
* By explicitly declaring this plugin compatible we:
* 1. Prevent WooCommerce from showing a “This plugin may not work with HPOS” warning.
* 2. Signal that we do NOT read/write postmeta for orders in a way that would break under HPOS.
* 3. Allow merchants to enable HPOS without fear of data corruption.
*
* The check for FeaturesUtil::class guarantees we only call the API when WC 7.1+ is present.
*/
public function declare_hpos_compatibility() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
public function init() {
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
$this->base_currency = get_woocommerce_currency();
$this->target_currency = $this->get_visitor_currency();
// Only apply if target currency is different from base currency
if ( $this->target_currency !== $this->base_currency ) {
add_filter( 'woocommerce_get_price_html', [ $this, 'format_local_price_html' ], 10, 2 );
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ] );
}
}
/**
* Detect visitor currency based on geolocation.
*/
private function get_visitor_currency() {
if ( ! class_exists( 'WC_Geolocation' ) ) {
return 'BDT'; // Default to BDT as requested
}
$location = WC_Geolocation::geolocate_ip();
$country = isset( $location['country'] ) ? strtoupper($location['country']) : 'BD';
if ( empty( $country ) ) {
$country = 'BD';
}
return $this->get_currency_by_country( $country );
}
/**
* Map country codes to currency codes.
*/
private function get_currency_by_country( $country ) {
$currency = [
'BD' => 'BDT',
'US' => 'USD',
'GB' => 'GBP',
'FR' => 'EUR',
'DE' => 'EUR',
'IT' => 'EUR',
'ES' => 'EUR',
'NL' => 'EUR',
'BE' => 'EUR',
'AT' => 'EUR',
'PT' => 'EUR',
'IE' => 'EUR',
'FI' => 'EUR',
'GR' => 'EUR',
'IN' => 'INR',
'AU' => 'AUD',
'CA' => 'CAD',
'SG' => 'SGD',
'MY' => 'MYR',
'JP' => 'JPY',
'CN' => 'CNY',
'PK' => 'PKR',
'LK' => 'LKR',
'NP' => 'NPR',
'AE' => 'AED',
'SA' => 'SAR',
'TR' => 'TRY',
'ZA' => 'ZAR',
'NZ' => 'NZD',
'CH' => 'CHF',
'SE' => 'SEK',
'NO' => 'NOK',
'DK' => 'DKK',
'HK' => 'HKD',
];
return isset( $currency[ $country ] ) ? $currency[ $country ] : 'BDT';
}
/**
* Fetch exchange rates with caching.
*/
private function get_exchange_rate() {
$cache_key = 'wc_local_currency_rate_' . $this->base_currency . '_' . $this->target_currency;
$rate = get_transient( $cache_key );
if ( false === $rate ) {
$response = wp_remote_get( $this->api_url . strtolower( $this->base_currency ) );
if ( is_wp_error( $response ) ) {
return 1;
}
$data = json_decode( wp_remote_retrieve_body( $response ), true );
if ( isset( $data['rates'][ $this->target_currency ] ) ) {
$rate = $data['rates'][ $this->target_currency ];
set_transient( $cache_key, $rate, DAY_IN_SECONDS );
} else {
$rate = 1;
}
}
return $rate;
}
/**
* Format the price HTML to include local currency.
*/
public function format_local_price_html( $price_html, $product ) {
if ( is_admin() || is_cart() || is_checkout() ) {
return $price_html;
}
$rate = $this->get_exchange_rate();
if ( 1 == $rate ) {
return $price_html;
}
if ( $product->is_type( 'variable' ) ) {
$min_price = $product->get_variation_price( 'min' );
$max_price = $product->get_variation_price( 'max' );
if ( $min_price !== $max_price ) {
$local_min = $min_price * $rate;
$local_max = $max_price * $rate;
$local_price_formatted = sprintf(
'%s – %s',
wc_price( $local_min, [ 'currency' => $this->target_currency ] ),
wc_price( $local_max, [ 'currency' => $this->target_currency ] )
);
} else {
$local_price_formatted = wc_price( $min_price * $rate, [ 'currency' => $this->target_currency ] );
}
} else {
$price = $product->get_price();
if ( ! $price ) {
return $price_html;
}
$local_price_formatted = wc_price( $price * $rate, [ 'currency' => $this->target_currency ] );
}
return sprintf(
'<span class="wc-local-price">%s</span> <small class="wc-original-price-bracket">(%s)</small>',
$local_price_formatted,
$price_html
);
}
/**
* Enqueue simple CSS for the bracketed price.
*/
public function enqueue_styles() {
$css = "
.wc-original-price-bracket {
font-size: 0.8em;
color: #777;
margin-left: 5px;
}
.wc-original-price-bracket ins {
text-decoration: none;
}
.wc-original-price-bracket del {
display: none; /* Hide original sale price in bracket to keep it clean */
}
";
wp_add_inline_style( 'woocommerce-general', $css );
}
}
WC_Local_Currency_Display::get_instance();