-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheuro.php
275 lines (208 loc) · 9.72 KB
/
euro.php
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/*
Plugin Name: Woocommerce HRK to EUR converter
Plugin URI: https://github.com/ikovacic/woocommerce-hrk-eur-converter
Description: Plugin for change the currency in woocommerce from HRK to EUR
Version: 1.0
WC tested up to: 7.2
Author URI: https://github.com/ikovacic/woocommerce-hrk-eur-converter
*/
if ( !defined('ABSPATH') ) {
die;
}
if ( !is_admin() ) return;
class Hrk2eur {
protected $plugin_name;
private $notices;
protected $config;
public function __construct() {
$this->plugin_name = 'hrk2eur';
$this->notices = array();
$this->config = array(
'fixed_rate' => 7.53450,
'precision' => 5,
);
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_hrk2eur_tab' ), 150 );
add_action( 'woocommerce_settings_hrk2eur_admin_tab', array( $this, 'hrk2eur_output_settings' ));
add_action( 'woocommerce_settings_save_hrk2eur_admin_tab', array( $this, 'hrk2eur_save' ));
add_action( 'admin_notices', array( $this, 'hrk2eur_notices' ));
}
public function get_plugin_name() {
return $this->plugin_name;
}
public static function add_hrk2eur_tab( $tabs ) {
$tabs['hrk2eur_admin_tab'] = __( 'HRK=>EUR', 'woocommerce' );
return $tabs;
}
public function hrk2eur_output_settings() {
$settings = array(
'section_title' => array(
'name' => __( 'Promjena valute trgovine iz HRK u EUR', 'woocommerce' ),
'type' => 'title',
'desc' => 'Klikom na gumb napravit ćete sljedeće promjene: Zadana valuta trgovine bit će EUR, a sve cijene proizvoda preračunat će se u EUR po fiksnom tečaju 7,53450. Također će se napraviti konverzija u dostavnim tarifama i kuponima, povijesti promjene cijena, te će se regenerirati lookup tablice. Ove promjene su trajne i NE MOGU se poništiti, pa se iz tog razloga strogo preporuča napraviti backup baze podataka prije pokretanja procesa! Nakon odrađene konverzije deaktivirajte i deinstalirajte plugin Hrk2Eur.',
'class' => 'hrk2eur-options-title',
'id' => 'hrk2eur_admin_tab_section_general_settings_title'
),
'hrk2eur_conversion' => array(
'name' => __( 'Napravi konverziju', 'woocommerce' ),
'type' => 'checkbox',
'default' => 'no',
'desc' => __( 'Označite box i spremite promjene da biste pokrenuli konverziju', 'woocommerce' ),
'id' => 'hrk2eur_conversion',
),
'section_end' => array(
'type' => 'sectionend',
'id' => 'hrk2eur_admin_tab_general_settings_end',
),
);
WC_Admin_Settings::output_fields( $settings );
}
function hrk2eur_save() {
if (isset($_POST['hrk2eur_conversion'])) {
$hrk2eur_conversion = sanitize_text_field($_POST['hrk2eur_conversion']);
if($hrk2eur_conversion) {
$this->applause_convert_currency();
}
}
}
function applause_hrk_to_eur( $value ) {
$config = $this->config;
return number_format( $value / $config['fixed_rate'], $config['precision'] );
}
// 01. Convert WooCommerce Settings ( HRK > EUR )
function applause_convert_options() {
global $wpdb;
if( $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}options SET option_value = %s WHERE option_name = %s",
'EUR',
'woocommerce_currency'
)
)) {
$this->notices[] = array( 'message' => __( 'Uspješno ažurirana valuta shopa', 'woocommerce' ), 'type' => 'success' );
} else {
$this->notices[] = array( 'message' => __( 'Nije uspjelo ažuriranje zadane valute shopa!', 'woocommerce' ), 'type' => 'error' );
}
}
// 02. Convert WooCommerce Shipping
function applause_convert_delivery() {
global $wpdb;
$keys = array();
// get all shipping methods
$results = $wpdb->get_results( "SELECT instance_id, method_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods", ARRAY_A );
// prepare option_names for shipping methods
foreach( $results as $result ) {
$keys[] = 'woocommerce_' . $result['method_id'] . '_' . $result['instance_id'] . '_settings';
}
// get settings for shipment methods
$results = $wpdb->get_results( "SELECT option_name, option_value FROM {$wpdb->prefix}options WHERE option_name IN ('" . implode("','", $keys) . "')", ARRAY_A );
$success = false;
foreach( $results as $result ) {
$shipping_settings = maybe_unserialize($result['option_value']);
// free_shipping
if( !empty( $shipping_settings['min_amount'] ) ) {
$shipping_settings['min_amount'] = $this->applause_hrk_to_eur( $shipping_settings['min_amount'] );
}
// local_pickup, flat_rate
if( !empty( $shipping_settings['cost'] ) ) {
$shipping_settings['cost'] = $this->applause_hrk_to_eur( $shipping_settings['cost'] );
}
if( update_option( $result['option_name'], $shipping_settings ) ) $success = true;
}
if($success){
$this->notices[] = array( 'message' => __( 'Uspješno ažurirane cijene dostave (prag za besplatnu dostavu i cijene dostave za svaku opciju)', 'woocommerce' ), 'type' => 'success' );
} else {
$this->notices[] = array( 'message' => __( 'Nisu sve opcije dostave uspješno ažurirane!', 'woocommerce' ), 'type' => 'warning' );
}
}
// 03. Convert WooCommerce Coupons
function applause_convert_coupons() {
global $wpdb;
$config = $this->config;
// convert min / max amounts on all coupons
$wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}postmeta SET meta_value = ROUND((meta_value/%f), %d) WHERE meta_key IN ('minimum_amount', 'maximum_amount')",
$config['fixed_rate'],
$config['precision']
)
);
$wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}postmeta m1
LEFT JOIN {$wpdb->prefix}postmeta m2
ON m1.post_id = m2.post_id
AND m2.meta_key = 'coupon_amount'
SET m2.meta_value = ROUND((m2.meta_value/%f), %d)
WHERE m1.meta_key = 'discount_type'
AND m1.meta_value IN ('fixed_cart', 'fixed_product')",
$config['fixed_rate'],
$config['precision']
)
);
$this->notices[] = array( 'message' => __( 'Uspješno ažurirani kuponi', 'woocommerce' ), 'type' => 'success' );
}
// 04. Convert WooCommerce product prices (including price history)
function applause_convert_products() {
global $wpdb;
$config = $this->config;
$wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}postmeta
SET meta_value = ROUND((meta_value/%f), %d)
WHERE meta_key IN ('_lowest_price_30_days', '_price', '_sale_price', '_regular_price', '_min_variation_price', '_max_variation_price', '_min_variation_regular_price', '_max_variation_regular_price', '_min_variation_sale_price', '_max_variation_sale_price')
AND meta_value != ''",
$config['fixed_rate'],
$config['precision']
)
);
$wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}price_history
SET price = ROUND((price/%f), %d)",
$config['fixed_rate'],
$config['precision']
)
);
$this->notices[] = array( 'message' => __( 'Cijene proizvoda uspješno ažurirane', 'woocommerce' ), 'type' => 'success' );
}
// 05. Recreate WooCommerce product lookup tables
function applause_lookup_tables() {
if ( function_exists( 'wc_update_product_lookup_tables_column' ) ) {
wc_update_product_lookup_tables_column( 'min_max_price' );
}
$this->notices[] = array( 'message' => __( 'Rekreiranje lookup tablica dovršeno', 'woocommerce' ), 'type' => 'success' );
}
// 06. Delete cached prices
function applause_delete_transients() {
global $wpdb;
$wpdb->query(
"DELETE FROM {$wpdb->prefix}options WHERE option_name LIKE '_transient_wc_var_prices_%' OR option_name LIKE '_transient_timeout_wc_var_prices_%'"
);
$this->notices[] = array( 'message' => __( 'Obrisani transienti', 'woocommerce' ), 'type' => 'success' );
}
function applause_convert_currency() {
$try = 'applause_currency';
$status = 'completed';
if ( get_option( $try ) != $status ) {
$this->applause_convert_options();
$this->applause_convert_delivery();
$this->applause_convert_coupons();
$this->applause_convert_products();
$this->applause_lookup_tables();
$this->applause_delete_transients();
// Clear cache (WP Super Cache only)
if ( function_exists( 'wp_cache_clean_cache' ) ) {
global $file_prefix;
wp_cache_clean_cache( $file_prefix, true );
}
update_option( $try, $status );
}
}
function hrk2eur_notices(){
foreach ($this->notices as $notice) {
echo '<div class="notice notice-' .esc_html($notice['type']). '"><p>' . esc_html($notice['message']) . '</p></div>';
}
}
}
$hrk2eur_plugin = new Hrk2eur();