PluginProbe ʕ •ᴥ•ʔ
WPML Multilingual & Multicurrency for WooCommerce / 5.5.7
WPML Multilingual & Multicurrency for WooCommerce v5.5.7
5.5.7 5.3.8 5.3.9 5.4.3 5.4.4 5.4.5 5.5.1 5.5.1.1 5.5.2.2 5.5.2.3 5.5.3 5.5.3.1 5.5.4 5.5.5 5.5.6 trunk 0.9 1.0 1.1 1.2 1.3 1.4 1.5 2.0 2.2 2.3 2.3.1 2.3.2 3.0 3.0.1 3.1 3.2 3.2.1 3.2.2 3.3 3.3.1 3.3.2 3.3.3 3.3.4 3.4 3.4.1 3.4.2 3.4.3 3.5 3.5.1 3.5.2 3.5.3 3.5.4 3.5.5 3.6 3.6.1 3.6.10 3.6.11 3.6.2 3.6.3 3.6.4 3.6.5 3.6.5.1 3.6.6 3.6.7 3.6.8 3.6.9 3.7 3.7.1 3.7.10 3.7.11 3.7.12 3.7.13 3.7.14 3.7.15 3.7.16 3.7.2 3.7.3 3.7.4 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.9.0 3.9.1 3.9.1.1 3.9.2 3.9.3 3.9.4 3.9.5 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.10.4 4.11.2 4.11.3.2 4.11.5 4.11.6 4.12.1 4.12.4 4.12.5 4.12.6 4.2.0 4.2.0.1 4.2.1 4.2.1.1 4.2.10 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.7.1 4.2.8 4.2.8.1 4.2.9 4.3.0 4.3.1 4.3.2 4.3.2.1 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0 4.4.1 4.4.2 4.4.2.1 4.5.0 4.6.0 4.6.1 4.6.2 4.6.2.1 4.6.3 4.6.5 4.6.6 4.6.7 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / compatibility / WcBookings / MulticurrencyHooks.php
woocommerce-multilingual / compatibility / WcBookings Last commit date
Templates 3 weeks ago TranslationEditor 3 weeks ago Calendar.php 1 year ago Emails.php 3 weeks ago Factory.php 3 weeks ago MulticurrencyHooks.php 3 weeks ago Prices.php 3 weeks ago SharedHooks.php 3 weeks ago Slots.php 3 weeks ago class-wcml-accommodation-bookings.php 3 weeks ago class-wcml-bookings.php 3 weeks ago
MulticurrencyHooks.php
774 lines
1 <?php
2
3 namespace WCML\Compatibility\WcBookings;
4
5 use WCML\Orders\Helper as OrdersHelper;
6
7 class MulticurrencyHooks implements \IWPML_Action {
8
9
10 private $woocommerce_wpml;
11
12 public function __construct( \woocommerce_wpml $woocommerce_wpml ) {
13 $this->woocommerce_wpml = $woocommerce_wpml;
14 }
15
16 public function add_hooks() {
17 add_action( 'woocommerce_bookings_after_booking_base_cost', [ $this, 'wcml_price_field_after_booking_base_cost' ] );
18 add_action( 'woocommerce_bookings_after_booking_block_cost', [ $this, 'wcml_price_field_after_booking_block_cost' ] );
19 add_action( 'woocommerce_bookings_after_display_cost', [ $this, 'wcml_price_field_after_display_cost' ] );
20 add_action( 'woocommerce_bookings_after_booking_pricing_base_cost', [ $this, 'wcml_price_field_after_booking_pricing_base_cost' ], 10, 2 );
21 add_action( 'woocommerce_bookings_after_booking_pricing_cost', [ $this, 'wcml_price_field_after_booking_pricing_cost' ], 10, 2 );
22 add_action( 'woocommerce_bookings_after_person_cost', [ $this, 'wcml_price_field_after_person_cost' ] );
23 add_action( 'woocommerce_bookings_after_person_block_cost', [ $this, 'wcml_price_field_after_person_block_cost' ] );
24 add_action( 'woocommerce_bookings_after_resource_cost', [ $this, 'wcml_price_field_after_resource_cost' ], 10, 2 );
25 add_action( 'woocommerce_bookings_after_resource_block_cost', [ $this, 'wcml_price_field_after_resource_block_cost' ], 10, 2 );
26
27 add_action( 'woocommerce_bookings_after_bookings_pricing', [ $this, 'after_bookings_pricing' ] );
28
29 add_action( 'save_post', [ $this, 'save_custom_costs' ], \WCML_Bookings::PRIORITY_SAVE_POST_ACTION - 1 );
30
31 add_filter( 'woocommerce_bookings_process_cost_rules_cost', [ $this, 'wc_bookings_process_cost_rules_cost' ], 10, 3 );
32 add_filter( 'woocommerce_bookings_process_cost_rules_base_cost', [ $this, 'wc_bookings_process_cost_rules_base_cost' ], 10, 3 );
33 add_filter( 'woocommerce_bookings_process_cost_rules_override_block', [ $this, 'wc_bookings_process_cost_rules_override_block_cost' ], 10, 3 );
34
35 add_action( 'woocommerce_bookings_after_create_booking_page', [ $this, 'booking_currency_dropdown' ] );
36 add_action( 'init', [ $this, 'set_booking_currency' ] );
37 add_action( 'wp_ajax_wcml_booking_set_currency', [ $this, 'set_booking_currency_ajax' ] );
38
39 add_action( 'woocommerce_bookings_create_booking_page_add_order_item', [ $this, 'set_order_currency_on_create_booking_page' ] );
40 add_filter( 'woocommerce_currency_symbol', [ $this, 'filter_booking_currency_symbol' ] );
41
42 add_filter( 'wcml_filter_currency_position', [ $this, 'create_booking_page_client_currency' ] );
43 add_filter( 'wcml_client_currency', [ $this, 'create_booking_page_client_currency' ] );
44
45 if ( ! is_admin() || isset( $_POST['action'] ) && 'wc_bookings_calculate_costs' === $_POST['action'] ) {
46 add_filter( 'get_post_metadata', [ $this, 'filter_wc_booking_cost' ], 10, 4 );
47 }
48 }
49
50 public function wcml_price_field_after_booking_base_cost( $postId ) {
51 $this->echo_wcml_price_field( $postId, 'wcml_wc_booking_cost' );
52 }
53
54 public function wcml_price_field_after_booking_block_cost( $postId ) {
55 if ( self::isWcBookingsBefore_1_10_9() ) {
56 $this->echo_wcml_price_field( $postId, 'wcml_wc_booking_base_cost' );
57 } else {
58 $this->echo_wcml_price_field( $postId, 'wcml_wc_booking_block_cost' );
59 }
60 }
61
62 public function wcml_price_field_after_display_cost( $postId ) {
63 $this->echo_wcml_price_field( $postId, 'wcml_wc_display_cost' );
64 }
65
66 public function wcml_price_field_after_booking_pricing_base_cost( $pricing, $postId ) {
67 $this->echo_wcml_price_field( $postId, 'wcml_wc_booking_pricing_base_cost', $pricing );
68 }
69
70 public function wcml_price_field_after_booking_pricing_cost( $pricing, $postId ) {
71 $this->echo_wcml_price_field( $postId, 'wcml_wc_booking_pricing_cost', $pricing );
72 }
73
74 public function wcml_price_field_after_person_cost( $personTypeId ) {
75 $this->echo_wcml_price_field( $personTypeId, 'wcml_wc_booking_person_cost', false, false );
76 }
77
78 public function wcml_price_field_after_person_block_cost( $personTypeId ) {
79 $this->echo_wcml_price_field( $personTypeId, 'wcml_wc_booking_person_block_cost', false, false );
80 }
81
82 public function wcml_price_field_after_resource_cost( $resourceId, $postId ) {
83 $this->echo_wcml_price_field( $postId, 'wcml_wc_booking_resource_cost', false, true, $resourceId );
84 }
85
86 public function wcml_price_field_after_resource_block_cost( $resourceId, $postId ) {
87 $this->echo_wcml_price_field( $postId, 'wcml_wc_booking_resource_block_cost', false, true, $resourceId );
88 }
89
90 public function echo_wcml_price_field( $postId, $field, $pricing = false, $check = true, $resourceId = false ) {
91 if ( ( ! $check || $this->woocommerce_wpml->products->is_original_product( $postId ) ) ) {
92
93 $currencies = $this->woocommerce_wpml->multi_currency->get_currencies();
94
95 $wc_currencies = get_woocommerce_currencies();
96
97 if ( ! function_exists( 'woocommerce_wp_text_input' ) ) {
98 include_once dirname( WC_PLUGIN_FILE ) . '/includes/admin/wc-meta-box-functions.php';
99 }
100
101 echo '<div class="wcml_custom_cost_field" >';
102
103 foreach ( $currencies as $currency_code => $currency ) {
104
105 switch ( $field ) {
106 case 'wcml_wc_booking_cost':
107 woocommerce_wp_text_input(
108 [
109 'id' => 'wcml_wc_booking_cost',
110 'class' => 'wcml_bookings_custom_price',
111 'name' => 'wcml_wc_booking_cost[' . $currency_code . ']',
112 'label' => get_woocommerce_currency_symbol( $currency_code ),
113 'description' => __( 'One-off cost for the booking as a whole.', 'woocommerce-bookings' ),
114 'value' => get_post_meta( $postId, '_wc_booking_cost_' . $currency_code, true ),
115 'type' => 'number',
116 'desc_tip' => true,
117 'custom_attributes' => [
118 'min' => '',
119 'step' => '0.01',
120 ],
121 ]
122 );
123 break;
124 case 'wcml_wc_booking_block_cost':
125 case 'wcml_wc_booking_base_cost':
126 $block_cost_key = '_wc_booking_base_cost_';
127 if ( $field === 'wcml_wc_booking_block_cost' ) {
128 $block_cost_key = '_wc_booking_block_cost_';
129 }
130 $block_cost_key .= $currency_code;
131 woocommerce_wp_text_input(
132 [
133 'id' => $field,
134 'class' => 'wcml_bookings_custom_price',
135 'name' => $field . '[' . $currency_code . ']',
136 'label' => get_woocommerce_currency_symbol( $currency_code ),
137 'description' => __( 'This is the cost per block booked. All other costs (for resources and persons) are added to this.', 'woocommerce-bookings' ),
138 'value' => get_post_meta( $postId, $block_cost_key, true ),
139 'type' => 'number',
140 'desc_tip' => true,
141 'custom_attributes' => [
142 'min' => '',
143 'step' => '0.01',
144 ],
145 ]
146 );
147 break;
148 case 'wcml_wc_display_cost':
149 woocommerce_wp_text_input(
150 [
151 'id' => 'wcml_wc_display_cost',
152 'class' => 'wcml_bookings_custom_price',
153 'name' => 'wcml_wc_display_cost[' . $currency_code . ']',
154 'label' => get_woocommerce_currency_symbol( $currency_code ),
155 'description' => __( 'The cost is displayed to the user on the frontend. Leave blank to have it calculated for you. If a booking has varying costs, this will be prefixed with the word "from:".', 'woocommerce-bookings' ),
156 'value' => get_post_meta( $postId, '_wc_display_cost_' . $currency_code, true ),
157 'type' => 'number',
158 'desc_tip' => true,
159 'custom_attributes' => [
160 'min' => '',
161 'step' => '0.01',
162 ],
163 ]
164 );
165 break;
166
167 case 'wcml_wc_booking_pricing_base_cost':
168 if ( isset( $pricing[ 'base_cost_' . $currency_code ] ) ) {
169 $value = $pricing[ 'base_cost_' . $currency_code ];
170 } else {
171 $value = '';
172 }
173
174 echo '<div class="wcml_bookings_range_block" >';
175 echo '<label>' . wp_kses_post( get_woocommerce_currency_symbol( $currency_code ) ) . '</label>';
176 echo '<input type="number" step="0.01" name="wcml_wc_booking_pricing_base_cost[' . esc_html( $currency_code ) . '][]" class="wcml_bookings_custom_price" value="' . esc_html( $value ) . '" placeholder="0" />';
177 echo '</div>';
178 break;
179
180 case 'wcml_wc_booking_pricing_cost':
181 if ( isset( $pricing[ 'cost_' . $currency_code ] ) ) {
182 $value = $pricing[ 'cost_' . $currency_code ];
183 } else {
184 $value = '';
185 }
186
187 echo '<div class="wcml_bookings_range_block" >';
188 echo '<label>' . wp_kses_post( get_woocommerce_currency_symbol( $currency_code ) ) . '</label>';
189 echo '<input type="number" step="0.01" name="wcml_wc_booking_pricing_cost[' . esc_html( $currency_code ) . '][]" class="wcml_bookings_custom_price" value="' . esc_html( $value ) . '" placeholder="0" />';
190 echo '</div>';
191 break;
192
193 case 'wcml_wc_booking_person_cost':
194 $value = get_post_meta( $postId, 'cost_' . $currency_code, true );
195
196 echo '<div class="wcml_bookings_person_block" >';
197 echo '<label>' . wp_kses_post( get_woocommerce_currency_symbol( $currency_code ) ) . '</label>';
198 echo '<input type="number" step="0.01" name="wcml_wc_booking_person_cost[' . intval( $postId ) . '][' . esc_html( $currency_code ) . ']" class="wcml_bookings_custom_price" value="' . esc_html( $value ) . '" placeholder="0" />';
199 echo '</div>';
200 break;
201
202 case 'wcml_wc_booking_person_block_cost':
203 $value = get_post_meta( $postId, 'block_cost_' . $currency_code, true );
204
205 echo '<div class="wcml_bookings_person_block" >';
206 echo '<label>' . wp_kses_post( get_woocommerce_currency_symbol( $currency_code ) ) . '</label>';
207 echo '<input type="number" step="0.01" name="wcml_wc_booking_person_block_cost[' . intval( $postId ) . '][' . esc_html( $currency_code ) . ']" class="wcml_bookings_custom_price" value="' . esc_html( $value ) . '" placeholder="0" />';
208 echo '</div>';
209 break;
210
211 case 'wcml_wc_booking_resource_cost':
212 $resource_base_costs = maybe_unserialize( get_post_meta( $postId, '_resource_base_costs', true ) );
213
214 if ( isset( $resource_base_costs['custom_costs'][ $currency_code ][ $resourceId ] ) ) {
215 $value = $resource_base_costs['custom_costs'][ $currency_code ][ $resourceId ];
216 } else {
217 $value = '';
218 }
219
220 echo '<div class="wcml_bookings_resource_block" >';
221 echo '<label>' . wp_kses_post( get_woocommerce_currency_symbol( $currency_code ) ) . '</label>';
222 echo '<input type="number" step="0.01" name="wcml_wc_booking_resource_cost[' . esc_html( $resourceId ) . '][' . esc_html( $currency_code ) . ']" class="wcml_bookings_custom_price" value="' . esc_html( $value ) . '" placeholder="0" />';
223 echo '</div>';
224 break;
225
226 case 'wcml_wc_booking_resource_block_cost':
227 $resource_block_costs = maybe_unserialize( get_post_meta( $postId, '_resource_block_costs', true ) );
228
229 if ( isset( $resource_block_costs['custom_costs'][ $currency_code ][ $resourceId ] ) ) {
230 $value = $resource_block_costs['custom_costs'][ $currency_code ][ $resourceId ];
231 } else {
232 $value = '';
233 }
234
235 echo '<div class="wcml_bookings_resource_block" >';
236 echo '<label>' . wp_kses_post( get_woocommerce_currency_symbol( $currency_code ) ) . '</label>';
237 echo '<input type="number" step="0.01" name="wcml_wc_booking_resource_block_cost[' . esc_html( $resourceId ) . '][' . esc_html( $currency_code ) . ']" class="wcml_bookings_custom_price" value="' . esc_html( $value ) . '" placeholder="0" />';
238 echo '</div>';
239 break;
240
241 default:
242 break;
243
244 }
245 }
246
247 echo '</div>';
248 }
249 }
250
251 public function after_bookings_pricing( $postId ) {
252
253 if ( in_array( 'booking', wp_get_post_terms( $postId, 'product_type', [ 'fields' => 'names' ] ) ) && $this->woocommerce_wpml->products->is_original_product( $postId ) ) {
254
255 $customCostsStatus = get_post_meta( $postId, Prices::CUSTOM_COSTS_STATUS_KEY, true );
256
257 $checked = ! $customCostsStatus ? 'checked="checked"' : ' ';
258
259 echo '<div class="wcml_custom_costs">';
260
261 echo '<input type="radio" name="_wcml_custom_costs" id="wcml_custom_costs_auto" value="0" class="wcml_custom_costs_input" ' . esc_html( $checked ) . ' />';
262 echo '<label for="wcml_custom_costs_auto">' . esc_html__( 'Calculate costs in other currencies automatically', 'woocommerce-multilingual' ) . '</label>';
263
264 $checked = 1 === (int) $customCostsStatus ? 'checked="checked"' : ' ';
265
266 echo '<input type="radio" name="_wcml_custom_costs" value="1" id="wcml_custom_costs_manually" class="wcml_custom_costs_input" ' . esc_html( $checked ) . ' />';
267 echo '<label for="wcml_custom_costs_manually">' . esc_html__( 'Set costs in other currencies manually', 'woocommerce-multilingual' ) . '</label>';
268
269 wp_nonce_field( 'wcml_save_custom_costs', '_wcml_custom_costs_nonce' );
270
271 echo '</div>';
272 }
273 }
274
275 public function save_custom_costs( $postId ) {
276 $nonce = filter_var( isset( $_POST['_wcml_custom_costs_nonce'] ) ? $_POST['_wcml_custom_costs_nonce'] : '', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
277
278 if ( isset( $_POST['_wcml_custom_costs'] ) && isset( $nonce ) && wp_verify_nonce( $nonce, 'wcml_save_custom_costs' ) ) {
279
280 update_post_meta( $postId, Prices::CUSTOM_COSTS_STATUS_KEY, $_POST['_wcml_custom_costs'] );
281
282 if ( 1 === (int) $_POST['_wcml_custom_costs'] ) {
283
284 $currencies = $this->woocommerce_wpml->multi_currency->get_currencies();
285 if ( empty( $currencies ) || 0 === $postId ) {
286 return;
287 }
288
289 $this->update_booking_costs( $currencies, $postId );
290 $this->update_booking_pricing( $currencies, $postId );
291
292 if ( isset( $_POST['wcml_wc_booking_person_cost'] ) && is_array( $_POST['wcml_wc_booking_person_cost'] ) ) {
293 $this->update_booking_person_cost( $currencies, $_POST['wcml_wc_booking_person_cost'] );
294 }
295
296 if ( isset( $_POST['wcml_wc_booking_person_block_cost'] ) && is_array( $_POST['wcml_wc_booking_person_block_cost'] ) ) {
297 $this->update_booking_person_block_cost( $currencies, $_POST['wcml_wc_booking_person_block_cost'] );
298 }
299
300 if ( isset( $_POST['wcml_wc_booking_resource_cost'] ) && is_array( $_POST['wcml_wc_booking_resource_cost'] ) ) {
301 $this->update_booking_resource_cost( $currencies, $postId, $_POST['wcml_wc_booking_resource_cost'] );
302 }
303
304 if ( isset( $_POST['wcml_wc_booking_resource_block_cost'] ) && is_array( $_POST['wcml_wc_booking_resource_block_cost'] ) ) {
305 $this->update_booking_resource_block_cost( $currencies, $postId, $_POST['wcml_wc_booking_resource_block_cost'] );
306 }
307
308 update_post_meta( $postId, '_price', '' );
309 }
310 }
311 }
312
313 private function update_booking_costs( $currencies = [], $postId = 0 ) {
314 $bookingOptions = [
315 'wcml_wc_booking_cost' => '_wc_booking_cost_',
316 'wcml_wc_booking_block_cost' => '_wc_booking_block_cost_',
317 'wcml_wc_display_cost' => '_wc_display_cost_',
318 ];
319
320 if ( self::isWcBookingsBefore_1_10_9() ) {
321 unset( $bookingOptions['wcml_wc_booking_block_cost'] );
322 $bookingOptions['wcml_wc_booking_base_cost'] = '_wc_booking_base_cost_';
323 }
324
325 foreach ( $currencies as $code => $currency ) {
326 foreach ( $bookingOptions as $bookingOptionsPostKey => $bookingOptionsMetaKeyPrefix ) {
327 if ( isset( $_POST[ $bookingOptionsPostKey ][ $code ] ) ) {
328 update_post_meta( $postId, $bookingOptionsMetaKeyPrefix . $code, sanitize_text_field( $_POST[ $bookingOptionsPostKey ][ $code ] ) );
329 }
330 }
331 }
332
333 return true;
334 }
335
336 private function update_booking_pricing( $currencies = [], $postId = 0 ) {
337 $updatedMeta = [];
338 $bookingPricing = get_post_meta( $postId, '_wc_booking_pricing', true );
339 if ( empty( $bookingPricing ) ) {
340 return false;
341 }
342
343 foreach ( $bookingPricing as $key => $prices ) {
344 $updatedMeta[ $key ] = $prices;
345 foreach ( $currencies as $code => $currency ) {
346 if ( isset( $_POST['wcml_wc_booking_pricing_base_cost'][ $code ][ $key ] ) ) {
347 $updatedMeta[ $key ][ 'base_cost_' . $code ] = sanitize_text_field( $_POST['wcml_wc_booking_pricing_base_cost'][ $code ][ $key ] );
348 }
349 if ( isset( $_POST['wcml_wc_booking_pricing_cost'][ $code ][ $key ] ) ) {
350 $updatedMeta[ $key ][ 'cost_' . $code ] = sanitize_text_field( $_POST['wcml_wc_booking_pricing_cost'][ $code ][ $key ] );
351 }
352 }
353 }
354
355 update_post_meta( $postId, '_wc_booking_pricing', $updatedMeta );
356
357 return true;
358 }
359
360 private function update_booking_person_cost( $currencies = [], $personCosts = [] ) {
361 if ( empty( $personCosts ) ) {
362 return false;
363 }
364
365 foreach ( $personCosts as $personId => $costs ) {
366 foreach ( $currencies as $code => $currency ) {
367 if ( isset( $costs[ $code ] ) ) {
368 update_post_meta( $personId, 'cost_' . $code, sanitize_text_field( $costs[ $code ] ) );
369 }
370 }
371 }
372
373 return true;
374 }
375
376 private function update_booking_person_block_cost( $currencies = [], $blockCosts = [] ) {
377 if ( empty( $blockCosts ) ) {
378 return false;
379 }
380
381 foreach ( $blockCosts as $personId => $costs ) {
382 foreach ( $currencies as $code => $currency ) {
383 if ( isset( $costs[ $code ] ) ) {
384 update_post_meta( $personId, 'block_cost_' . $code, sanitize_text_field( $costs[ $code ] ) );
385 }
386 }
387 }
388
389 return true;
390 }
391
392 private function update_booking_resource_cost( $currencies = [], $postId = 0, $resourceCost = [] ) {
393 if ( empty( $resourceCost ) ) {
394 return false;
395 }
396
397 $updatedMeta = get_post_meta( $postId, '_resource_base_costs', true );
398 if ( ! is_array( $updatedMeta ) ) {
399 $updatedMeta = [];
400 }
401
402 $wcBookingResourceCosts = [];
403
404 foreach ( $resourceCost as $resourceId => $costs ) {
405
406 foreach ( $currencies as $code => $currency ) {
407
408 if ( isset( $costs[ $code ] ) ) {
409 $wcBookingResourceCosts[ $code ][ $resourceId ] = sanitize_text_field( $costs[ $code ] );
410 }
411 }
412 }
413
414 $updatedMeta['custom_costs'] = $wcBookingResourceCosts;
415
416 update_post_meta( $postId, '_resource_base_costs', $updatedMeta );
417
418 self::triggerActionResourceCostsUpdated( $postId, '_resource_base_costs' );
419
420 return true;
421 }
422
423 private function update_booking_resource_block_cost( $currencies = [], $postId = 0, $resourceBlockCost = [] ) {
424 if ( empty( $resourceBlockCost ) ) {
425 return false;
426 }
427
428 $updatedMeta = get_post_meta( $postId, '_resource_block_costs', true );
429 if ( ! is_array( $updatedMeta ) ) {
430 $updatedMeta = [];
431 }
432
433 $wc_booking_resource_block_costs = [];
434
435 foreach ( $resourceBlockCost as $resource_id => $costs ) {
436
437 foreach ( $currencies as $code => $currency ) {
438
439 if ( isset( $costs[ $code ] ) ) {
440 $wc_booking_resource_block_costs[ $code ][ $resource_id ] = sanitize_text_field( $costs[ $code ] );
441 }
442 }
443 }
444
445 $updatedMeta['custom_costs'] = $wc_booking_resource_block_costs;
446
447 update_post_meta( $postId, '_resource_block_costs', $updatedMeta );
448
449 self::triggerActionResourceCostsUpdated( $postId, '_resource_block_costs' );
450
451 return true;
452 }
453
454 private static function triggerActionResourceCostsUpdated( $postId, $key ) {
455 do_action( 'wcml_bookings_resource_costs_updated', $postId, $key );
456 }
457
458 public function wc_bookings_process_cost_rules_cost( $cost, $fields, $key ) {
459 return $this->filter_pricing_cost( $cost, $fields, 'cost_', $key );
460 }
461
462 public function wc_bookings_process_cost_rules_base_cost( $base_cost, $fields, $key ) {
463 return $this->filter_pricing_cost( $base_cost, $fields, 'base_cost_', $key );
464 }
465
466 public function wc_bookings_process_cost_rules_override_block_cost( $override_cost, $fields, $key ) {
467 return $this->filter_pricing_cost( $override_cost, $fields, 'override_block_', $key );
468 }
469
470 public function filter_pricing_cost( $cost, $fields, $name, $key ) {
471 $currency = $this->woocommerce_wpml->multi_currency->get_client_currency();
472
473 if ( $currency === wcml_get_woocommerce_currency_option() ) {
474 return $cost;
475 }
476
477 if ( isset( $_POST['form'] ) ) {
478 parse_str( $_POST['form'], $posted );
479
480 $booking_id = $posted['add-to-cart'];
481
482 } elseif ( isset( $_POST['add-to-cart'] ) ) {
483
484 $booking_id = $_POST['add-to-cart'];
485
486 }
487
488 if ( isset( $booking_id ) ) {
489 $original_id = $this->woocommerce_wpml->products->get_original_product_id( $booking_id );
490
491 if ( $booking_id != $original_id ) {
492 $fields = maybe_unserialize( get_post_meta( $original_id, '_wc_booking_pricing', true ) );
493 $fields = $fields[ $key ];
494 }
495 }
496
497 $needs_filter_pricing_cost = $this->needs_filter_pricing_cost( $name, $fields );
498
499 if ( $needs_filter_pricing_cost ) {
500 if ( isset( $fields[ $name . $currency ] ) ) {
501 return $fields[ $name . $currency ];
502 } else {
503 return $this->woocommerce_wpml->multi_currency->prices->convert_price_amount( $cost, $currency );
504 }
505 }
506
507 return $cost;
508 }
509
510 public function needs_filter_pricing_cost( $name, $fields ) {
511
512 $modifier_skip_values = [ 'divide', 'times' ];
513
514 if (
515 'override_block_' === $name ||
516 ( 'cost_' === $name && ! in_array( $fields['modifier'], $modifier_skip_values ) ) ||
517 ( 'base_cost_' === $name && ! in_array( $fields['base_modifier'], $modifier_skip_values ) )
518 ) {
519 return true;
520 } else {
521 return false;
522 }
523 }
524
525 public function booking_currency_dropdown() {
526 $current_booking_currency = $this->get_cookie_booking_currency();
527
528 $wc_currencies = get_woocommerce_currencies();
529 $currencies = $this->woocommerce_wpml->multi_currency->get_currencies( true );
530 ?>
531 <tr valign="top">
532 <th scope="row"><?php _e( 'Booking currency', 'woocommerce-multilingual' ); ?></th>
533 <td>
534 <select id="dropdown_booking_currency">
535 <?php foreach ( $currencies as $currency => $count ) : ?>
536 <option
537 value="<?php echo esc_html( $currency ); ?>" <?php echo $current_booking_currency == $currency ? 'selected="selected"' : ''; ?>><?php echo esc_html( $wc_currencies[ $currency ] ); ?></option>
538 <?php endforeach; ?>
539 </select>
540 </td>
541 </tr>
542
543 <?php
544
545 $wcml_booking_set_currency_nonce = esc_js( wp_create_nonce( 'booking_set_currency' ) );
546 $wcml_booking_set_currency_script = <<<JS
547 jQuery(document).on('change', '#dropdown_booking_currency', function(){
548 jQuery.ajax({
549 url: ajaxurl,
550 type: 'post',
551 data: {
552 action: 'wcml_booking_set_currency',
553 currency: jQuery('#dropdown_booking_currency').val(),
554 wcml_nonce: '$wcml_booking_set_currency_nonce'
555 },
556 success: function( response ){
557 if(typeof response.error !== 'undefined'){
558 alert(response.error);
559 }else{
560 window.location = window.location.href;
561 }
562 }
563 })
564 });
565 JS;
566
567 $handle = 'wcml_booking_set_currency_dropdown';
568 wp_register_script( $handle, '', [ 'jquery' ], WCML_VERSION, true );
569 wp_enqueue_script( $handle );
570 wp_add_inline_script( $handle, $wcml_booking_set_currency_script );
571 }
572
573 public function set_booking_currency_ajax() {
574 $nonce = filter_input( INPUT_POST, 'wcml_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
575 if ( ! $nonce || ! wp_verify_nonce( $nonce, 'booking_set_currency' ) ) {
576 echo json_encode( [ 'error' => __( 'Invalid nonce', 'woocommerce-multilingual' ) ] );
577 die();
578 }
579
580 $this->set_booking_currency( filter_input( INPUT_POST, 'currency', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
581
582 die();
583 }
584
585 public function set_booking_currency( $currency_code = false ) {
586 $cookie_name = '_wcml_booking_currency';
587
588 if ( ! isset( $_COOKIE[ $cookie_name ] ) && ! headers_sent() ) {
589 $currency_code = $this->woocommerce_wpml->multi_currency->get_currency_code();
590 }
591
592 if ( $currency_code ) {
593 setcookie( $cookie_name, $currency_code, time() + 86400, COOKIEPATH, COOKIE_DOMAIN );
594 }
595 }
596
597 public function get_cookie_booking_currency() {
598 if ( isset( $_COOKIE ['_wcml_booking_currency'] ) ) {
599 $currency = $_COOKIE['_wcml_booking_currency'];
600 } else {
601 $currency = wcml_get_woocommerce_currency_option();
602 }
603
604 return $currency;
605 }
606
607 public function set_order_currency_on_create_booking_page( $order_id ) {
608 OrdersHelper::setCurrency( $order_id, $this->get_cookie_booking_currency() );
609 }
610
611 public function filter_booking_currency_symbol( $currency ) {
612 global $pagenow;
613
614 remove_filter( 'woocommerce_currency_symbol', [ $this, 'filter_booking_currency_symbol' ] );
615 if ( isset( $_COOKIE ['_wcml_booking_currency'] ) && $pagenow == 'edit.php' && isset( $_GET['page'] ) && $_GET['page'] == 'create_booking' ) {
616 $currency = get_woocommerce_currency_symbol( $_COOKIE ['_wcml_booking_currency'] );
617 }
618 add_filter( 'woocommerce_currency_symbol', [ $this, 'filter_booking_currency_symbol' ] );
619
620 return $currency;
621 }
622
623 public function create_booking_page_client_currency( $currency ) {
624 global $pagenow;
625
626 if ( wpml_is_ajax() && isset( $_POST['form'] ) ) {
627 parse_str( $_POST['form'], $posted );
628 }
629
630 if ( ( $pagenow == 'edit.php' && isset( $_GET['page'] ) && $_GET['page'] == 'create_booking' ) || ( isset( $posted['_wp_http_referer'] ) && strpos( $posted['_wp_http_referer'], 'page=create_booking' ) !== false ) ) {
631 $currency = $this->get_cookie_booking_currency();
632 }
633
634 return $currency;
635 }
636
637 public function filter_wc_booking_cost( $check, $object_id, $meta_key, $single ) {
638
639 if ( in_array(
640 $meta_key,
641 [
642 '_wc_booking_cost',
643 '_wc_booking_base_cost',
644 '_wc_display_cost',
645 '_wc_booking_pricing',
646 'cost',
647 '_wc_booking_block_cost',
648 'block_cost',
649 '_resource_base_costs',
650 '_resource_block_costs',
651 ]
652 ) ) {
653
654 $original_id = $this->woocommerce_wpml->products->get_original_product_id( $object_id );
655
656 $cost_status = get_post_meta( $original_id, Prices::CUSTOM_COSTS_STATUS_KEY, true );
657
658 $currency = $this->woocommerce_wpml->multi_currency->get_client_currency();
659
660 if ( $currency === wcml_get_woocommerce_currency_option() ) {
661 return $check;
662 }
663
664 if ( in_array( $meta_key, [ 'cost', 'block_cost' ] ) ) {
665
666 if ( get_post_type( $object_id ) == 'bookable_person' ) {
667
668 $original_id = apply_filters( 'wpml_object_id', wp_get_post_parent_id( $object_id ), 'product', true, $this->woocommerce_wpml->products->get_original_product_language( wp_get_post_parent_id( $object_id ) ) );
669 $cost_status = get_post_meta( $original_id, Prices::CUSTOM_COSTS_STATUS_KEY, true );
670
671 $value = get_post_meta( $object_id, $meta_key . '_' . $currency, true );
672
673 if ( $cost_status && $value ) {
674
675 return $value;
676
677 } else {
678
679 remove_filter( 'get_post_metadata', [ $this, 'filter_wc_booking_cost' ], 10 );
680
681 $cost = get_post_meta( $object_id, $meta_key, true );
682
683 add_filter( 'get_post_metadata', [ $this, 'filter_wc_booking_cost' ], 10, 4 );
684
685 return $this->woocommerce_wpml->multi_currency->prices->convert_price_amount( $cost, $currency );
686 }
687 } else {
688
689 return $check;
690
691 }
692 }
693
694 if ( in_array(
695 $meta_key,
696 [
697 '_wc_booking_pricing',
698 '_resource_base_costs',
699 '_resource_block_costs',
700 ]
701 ) ) {
702
703 remove_filter( 'get_post_metadata', [ $this, 'filter_wc_booking_cost' ], 10 );
704
705 if ( $meta_key == '_wc_booking_pricing' ) {
706
707 if ( $original_id != $object_id ) {
708 $value = get_post_meta( $original_id, $meta_key );
709 } else {
710 $value = $check;
711 }
712 } else {
713
714 $costs = maybe_unserialize( get_post_meta( $object_id, $meta_key, true ) );
715
716 if ( ! $costs ) {
717 $value = $check;
718 } elseif ( $cost_status && isset( $costs['custom_costs'][ $currency ] ) ) {
719
720 $res_costs = [];
721 foreach ( $costs['custom_costs'][ $currency ] as $resource_id => $cost ) {
722 $trns_resource_id = apply_filters( 'wpml_object_id', $resource_id, 'bookable_resource', true );
723 $res_costs[ $trns_resource_id ] = $cost;
724 }
725 $value = [ 0 => $res_costs ];
726 } elseif ( $cost_status && isset( $costs[0]['custom_costs'][ $currency ] ) ) {
727 $value = [ 0 => $costs[0]['custom_costs'][ $currency ] ];
728 } else {
729
730 $converted_values = [];
731
732 foreach ( $costs as $resource_id => $cost ) {
733 $converted_values[0][ $resource_id ] = $this->woocommerce_wpml->multi_currency->prices->convert_price_amount( $cost, $currency );
734 }
735
736 $value = $converted_values;
737 }
738 }
739
740 add_filter( 'get_post_metadata', [ $this, 'filter_wc_booking_cost' ], 10, 4 );
741
742 return $value;
743
744 }
745
746 $value = get_post_meta( $original_id, $meta_key . '_' . $currency, true );
747
748 if ( $cost_status && ( ! empty( $value ) || ( empty( $value ) && $meta_key == '_wc_display_cost' ) ) ) {
749
750 return $value;
751
752 } else {
753
754 remove_filter( 'get_post_metadata', [ $this, 'filter_wc_booking_cost' ], 10 );
755
756 $value = get_post_meta( $original_id, $meta_key, true );
757
758 $value = $this->woocommerce_wpml->multi_currency->prices->convert_price_amount( $value, $currency );
759
760 add_filter( 'get_post_metadata', [ $this, 'filter_wc_booking_cost' ], 10, 4 );
761
762 return $value;
763
764 }
765 }
766
767 return $check;
768 }
769
770 public static function isWcBookingsBefore_1_10_9() {
771 return version_compare( WC_BOOKINGS_VERSION, '1.10.9', '<' );
772 }
773 }
774