-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpmpro-multiple-currencies.php
219 lines (176 loc) · 8.03 KB
/
pmpro-multiple-currencies.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
<?php
/**
* Plugin Name: Multiple Currencies for Paid Memberships Pro
* Description: Change currencies depending on Paid Memberships Pro level.
* Version: 0.0.5
* Author: Louis Wolmarans
* Author URI: https://letmefreelance.co.za/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
// Add a currency setting on the level edit screen
function lmf_action_pmpro_membership_level_after_other_settings() {
global $pmpro_currencies;
if ( isset( $_REQUEST['edit'] ) && '-1' !== $_REQUEST['edit'] ) {
$edit = intval( $_REQUEST['edit'] );
$selected_custom_currency = get_pmpro_membership_level_meta( $edit, 'pmpro_custom_currency', true );
} else {
$selected_custom_currency = "DEFAULT";
}
?>
<h2 class="title"><?php esc_html_e( 'Currency Settings', 'pmpro-multiple-currencies' ); ?></h2>
<table class="form-table">
<tbody>
<tr>
<th scope="row" valign="top">
<label><?php _e('Choose a currency for this membership level', 'pmpro-multiple-currencies' );?>:</label>
</th>
<td>
<select name="pmpro_custom_currency">
<option value="DEFAULT" <?php if ( $selected_custom_currency == "DEFAULT" ) { ?>selected="selected"<?php } ?>><?php _e( 'Use the default currency', 'pmpro-multiple-currencies' ); ?></option>
<?php
foreach ( $pmpro_currencies as $key => $val) {
if ( is_array( $val ) ) {
$cdescription = $val['name'];
$custom_symbol = ( !empty( $val['symbol'] ) ) ? $val['symbol'] : $val['name'];
} else {
$cdescription = $val;
$custom_symbol = $key;
if ( strpos( $cdescription, '(' ) !== false && strpos( $cdescription, ')' ) !== false ) {
preg_match('#\((.*?)\)#', $cdescription, $match);
$custom_symbol = $match[1];
}
}
$selected = "";
if ( $selected_custom_currency !== "DEFAULT" ) {
$c_arr = explode( ",", $selected_custom_currency );
if ( $c_arr[0] == $key ) {
$selected = "selected";
}
}
$option_value = $key.",".$custom_symbol;
?>
<option value="<?php echo esc_attr( $option_value ); ?>" <?php echo $selected; ?>><?php echo $cdescription; ?></option>
<?php
}
?>
</select>
<p class="description"><?php _e( 'Make sure your payment gateway supports the selected currency.', 'pmpro-multiple-currencies' ); ?></p>
</td>
</tr>
</tbody>
</table>
<?php
}
add_action( "pmpro_membership_level_after_other_settings", "lmf_action_pmpro_membership_level_after_other_settings", 10, 0 );
// save the custom currency setting in level meta
function lmf_pmpro_save_membership_level( $level_id ) {
if ( $level_id <= 0 ) return;
$pmpro_custom_currency = sanitize_text_field( $_REQUEST['pmpro_custom_currency'] );
update_pmpro_membership_level_meta( $level_id, 'pmpro_custom_currency', $pmpro_custom_currency );
}
add_action( "pmpro_save_membership_level", "lmf_pmpro_save_membership_level" );
// main function to check for a currency level and update currencies
function lmf_update_currency_per_level( $level_id ) {
global $pmpro_currency, $pmpro_currency_symbol, $level_currencies;
$selected_custom_currency = get_pmpro_membership_level_meta( $level_id, 'pmpro_custom_currency', true );
if (
$selected_custom_currency !== false &&
! empty ( $selected_custom_currency ) &&
$selected_custom_currency !== "DEFAULT"
) {
$level_currency = explode( ",", $selected_custom_currency );
$pmpro_currency = $level_currency[0];
if ( empty ( $level_currency[1] ) ) {
$pmpro_currency_symbol = $level_currency[0];
} else {
$pmpro_currency_symbol = $level_currency[1];
}
}
}
// change currency on checkout page
function lmf_pmpro_checkout_level( $level ) {
lmf_update_currency_per_level( $level->id );
return $level;
}
add_filter( "pmpro_checkout_level", "lmf_pmpro_checkout_level" );
// change currency when sent as a request param
function lmf_init_currency_check() {
if ( ! empty( $_REQUEST['level'] ) )
return lmf_update_currency_per_level( intval( $_REQUEST['level'] ) );
}
add_action( "init", "lmf_init_currency_check" );
// params in the admin
function lmf_admin_init_currency_check() {
if (
! empty( $_REQUEST['edit'] ) &&
! empty( $_REQUEST['page'] ) &&
'pmpro-membershiplevels' == $_REQUEST['page'] &&
'-1' !== $_REQUEST['edit']
)
return lmf_update_currency_per_level( intval( $_REQUEST['edit'] ) );
}
add_action( "admin_init", "lmf_admin_init_currency_check" );
function lmf_pmpro_level_cost_text( $r, $level, $tags, $short ) {
global $pmpro_currency, $pmpro_currency_symbol, $pmpro_currencies;
$selected_custom_currency = get_pmpro_membership_level_meta( $level->id, 'pmpro_custom_currency', true );
if (
$selected_custom_currency !== false &&
! empty ( $selected_custom_currency ) &&
$selected_custom_currency !== "DEFAULT"
) {
$level_currency = explode( ",", $selected_custom_currency );
$curr = empty( $level_currency[1] ) ? $level_currency[0] : $level_currency[1];
return str_replace($pmpro_currency_symbol, $curr, $r);
}
return $r;
}
add_filter( "pmpro_level_cost_text", "lmf_pmpro_level_cost_text", 10, 4 );
function lmf_pmpro_amounts_filter( $total, $data ) {
$level_id = (int) $data->membership_id;
$custom_currency = explode( ',', get_pmpro_membership_level_meta( $level_id, 'pmpro_custom_currency', true ) );
if ( ! empty( $custom_currency[ 1 ] ) ) {
global $pmpro_currency_symbol;
$custom_currency_symbol = html_entity_decode( $pmpro_currency_symbol );
$total = html_entity_decode( $total );
$total = str_replace( $custom_currency_symbol, $custom_currency [ 1 ], $total );
}
return $total;
}
add_filter( 'pmpro_order_formatted_total', 'lmf_pmpro_amounts_filter', 10, 2 );
function lmf_subscription_formatted_cost_text( $total, $data ) {
$level_id = (int) $data->get_membership_level_id();
$custom_currency = explode( ',', get_pmpro_membership_level_meta( $level_id, 'pmpro_custom_currency', true ) );
if ( ! empty( $custom_currency[ 1 ] ) ) {
global $pmpro_currency_symbol;
$custom_currency_symbol = html_entity_decode( $pmpro_currency_symbol );
$total = html_entity_decode( $total );
$total = str_replace( $custom_currency_symbol, $custom_currency [ 1 ], $total );
}
return $total;
}
add_filter( 'pmpro_subscription_cost_text', 'lmf_subscription_formatted_cost_text', 10, 2 );
function lmf_pmpro_after_order_settings_table( $order ) {
$level_id = (int) $order->membership_id;
$custom_currency = explode( ',', get_pmpro_membership_level_meta( $level_id, 'pmpro_custom_currency', true ) );
if ( empty( $custom_currency[ 1 ] ) ) {
return;
}
?>
<div class="pmpro_section" data-visibility="shown" data-activated="true">
<div class="pmpro_section_inside">
<table class="form-table">
<tbody>
<tr>
<th scope="row" valign="top"><label for="notes">Currency</label></th>
<td>
<?php echo $custom_currency [ 1 ]; ?>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<?php
}
add_action( "pmpro_after_order_settings_table", "lmf_pmpro_after_order_settings_table" );