PluginProbe
PostNL for WooCommerce / 2.5.1
PostNL for WooCommerce v2.5.1
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / includes / class-wcmp-nlpostcode-fields.php

class-wcmp-nlpostcode-fields.php in PostNL for WooCommerce 2.5.1, at includes/class-wcmp-nlpostcode-fields.php

870 lines 29.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 use WPO\WC\PostNL\Compatibility\WC_Core as WCX;
3 use WPO\WC\PostNL\Compatibility\Order as WCX_Order;
4 use WPO\WC\PostNL\Compatibility\Product as WCX_Product;
5
6 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
7
8 if ( !class_exists( 'WC_NLPostcode_Fields' ) ) :
9
10 class WC_NLPostcode_Fields {
11
12 public $version = '1.5.4';
13
14 /**
15 * Construct.
16 */
17
18 public function __construct() {
19 // Load styles & scripts
20 add_action( 'wp_enqueue_scripts', array( &$this, 'add_styles_scripts' ) );
21 add_action( 'admin_enqueue_scripts', array( &$this, 'admin_scripts_styles' ) );
22
23 // set later priority for woocommerce_billing_fields / woocommerce_shipping_fields
24 // when Checkout Field Editor is active
25 if ( function_exists('thwcfd_is_locale_field') || function_exists('wc_checkout_fields_modify_billing_fields') ) {
26 add_filter( 'nl_checkout_fields_priority', function(){
27 return 1001;
28 } );
29 }
30
31 // Add street name & house number checkout fields.
32 if ( version_compare( WOOCOMMERCE_VERSION, '2.0' ) >= 0 ) {
33 // WC 2.0 or newer is used, the filter got a $coutry parameter, yay!
34 add_filter( 'woocommerce_billing_fields', array( &$this, 'nl_billing_fields' ), apply_filters( 'nl_checkout_fields_priority', 10, 'billing' ), 2 );
35 add_filter( 'woocommerce_shipping_fields', array( &$this, 'nl_shipping_fields' ), apply_filters( 'nl_checkout_fields_priority', 10, 'shipping' ), 2 );
36 } else {
37 // Backwards compatibility
38 add_filter( 'woocommerce_billing_fields', array( &$this, 'nl_billing_fields' ) );
39 add_filter( 'woocommerce_shipping_fields', array( &$this, 'nl_shipping_fields' ) );
40 }
41
42
43 // Hide state field for countries without states (backwards compatible fix for bug #4223)
44 if ( version_compare( WOOCOMMERCE_VERSION, '2.1', '<' ) ) {
45 add_filter( 'woocommerce_countries_allowed_country_states', array( &$this, 'hide_states' ) );
46 }
47
48 // Localize checkout fields (limit custom checkout fields to NL)
49 add_filter( 'woocommerce_country_locale_field_selectors', array( &$this, 'country_locale_field_selectors' ) );
50 add_filter( 'woocommerce_default_address_fields', array( &$this, 'default_address_fields' ) );
51 add_filter( 'woocommerce_get_country_locale', array( &$this, 'woocommerce_locale_nl' ), 1, 1);
52
53 // Load custom order data.
54 add_filter( 'woocommerce_load_order_data', array( &$this, 'load_order_data' ) );
55
56 // Custom shop_order details.
57 add_filter( 'woocommerce_admin_billing_fields', array( &$this, 'admin_billing_fields' ) );
58 add_filter( 'woocommerce_admin_shipping_fields', array( &$this, 'admin_shipping_fields' ) );
59 add_filter( 'woocommerce_found_customer_details', array( $this, 'customer_details_ajax' ) );
60 add_action( 'save_post', array( &$this,'save_custom_fields' ) );
61
62 // add to user profile page
63 add_filter( 'woocommerce_customer_meta_fields', array( &$this, 'user_profile_fields' ) );
64
65 // Processing checkout
66 add_filter( 'woocommerce_validate_postcode', array( &$this, 'validate_postcode' ), 10, 3 );
67
68 add_action('woocommerce_checkout_update_order_meta', array( &$this, 'merge_street_number_suffix' ), 20, 2 );
69 add_filter('woocommerce_process_checkout_field_billing_postcode', array( &$this, 'clean_billing_postcode' ) );
70 add_filter('woocommerce_process_checkout_field_shipping_postcode', array( &$this, 'clean_shipping_postcode' ) );
71
72 // Save the order data in WooCommerce 2.2 or later.
73 if ( version_compare( WOOCOMMERCE_VERSION, '2.2' ) >= 0 ) {
74 add_action( 'woocommerce_checkout_update_order_meta', array( &$this, 'save_order_data' ), 10, 2 );
75 }
76
77 // Remove placeholder values (IE8 & 9)
78 add_action('woocommerce_checkout_update_order_meta', array( &$this, 'remove_placeholders' ), 10, 2 );
79
80 // Fix weird required field translations
81 add_filter( 'woocommerce_checkout_required_field_notice', array( &$this, 'required_field_notices' ), 10, 2 );
82
83 $this->load_woocommerce_filters();
84 }
85
86 public function load_woocommerce_filters() {
87 // Custom address format.
88 if ( version_compare( WOOCOMMERCE_VERSION, '2.0.6', '>=' ) ) {
89 add_filter( 'woocommerce_localisation_address_formats', array( $this, 'localisation_address_formats' ) );
90 add_filter( 'woocommerce_formatted_address_replacements', array( $this, 'formatted_address_replacements' ), 1, 2 );
91 add_filter( 'woocommerce_order_formatted_billing_address', array( $this, 'order_formatted_billing_address' ), 1, 2 );
92 add_filter( 'woocommerce_order_formatted_shipping_address', array( $this, 'order_formatted_shipping_address' ), 1, 2 );
93 add_filter( 'woocommerce_user_column_billing_address', array( $this, 'user_column_billing_address' ), 1, 2 );
94 add_filter( 'woocommerce_user_column_shipping_address', array( $this, 'user_column_shipping_address' ), 1, 2 );
95 add_filter( 'woocommerce_my_account_my_address_formatted_address', array( $this, 'my_account_my_address_formatted_address' ), 1, 3 );
96 }
97
98 }
99
100 /**
101 * Load styles & scripts.
102 */
103 public function add_styles_scripts(){
104 if ( is_checkout() || is_account_page() ) {
105 if ( version_compare( WOOCOMMERCE_VERSION, '2.1', '<=' ) ) {
106 // Backwards compatibility for https://github.com/woothemes/woocommerce/issues/4239
107 wp_register_script(
108 'nl-checkout',
109 WooCommerce_PostNL()->plugin_url() . '/assets/js/nl-checkout.js',
110 array( 'wc-checkout' ),
111 $this->version
112 );
113 wp_enqueue_script( 'nl-checkout' );
114 }
115
116 if ( is_account_page() ) {
117 // Disable regular address fields for NL on account page - Fixed in WC 2.1 but not on init...
118 wp_register_script(
119 'nl-account-page',
120 WooCommerce_PostNL()->plugin_url() . '/assets/js/nl-account-page.js',
121 array( 'jquery' ),
122 $this->version
123 );
124 wp_enqueue_script( 'nl-account-page' );
125 }
126
127 wp_enqueue_style( 'nl-checkout', WooCommerce_PostNL()->plugin_url() . '/assets/css/nl-checkout.css' );
128 }
129
130 }
131
132 /**
133 * Load admin styles & scripts.
134 */
135 public function admin_scripts_styles ( $hook ) {
136 global $post_type;
137 if ( $post_type == 'shop_order' ) {
138 wp_enqueue_style(
139 'nl-checkout-admin',
140 WooCommerce_PostNL()->plugin_url() . '/assets/css/nl-checkout-admin.css',
141 array(), // deps
142 $this->version
143 );
144 }
145 }
146
147 /**
148 * Hide default Dutch address fields
149 * @param array $locale woocommerce country locale field settings
150 * @return array $locale
151 */
152 public function woocommerce_locale_nl( $locale ) {
153 $locale['NL']['address_1'] = array(
154 'required' => false,
155 'hidden' => true,
156 );
157
158 $locale['NL']['address_2'] = array(
159 'hidden' => true,
160 );
161
162 $locale['NL']['state'] = array(
163 'hidden' => true,
164 'required' => false,
165 );
166
167 $locale['NL']['street_name'] = array(
168 'required' => true,
169 'hidden' => false,
170 );
171
172 $locale['NL']['house_number'] = array(
173 'required' => true,
174 'hidden' => false,
175 );
176
177 $locale['NL']['house_number_suffix'] = array(
178 'required' => false,
179 'hidden' => false,
180 );
181
182 return $locale;
183 }
184
185 public function nl_billing_fields( $fields, $country = '' ) {
186 return $this->nl_checkout_fields( $fields, $country, 'billing');
187 }
188
189 public function nl_shipping_fields( $fields, $country = '' ) {
190 return $this->nl_checkout_fields( $fields, $country, 'shipping');
191 }
192
193 /**
194 * New checkout billing/shipping fields
195 * @param array $fields Default fields.
196 * @return array $fields New fields.
197 */
198 public function nl_checkout_fields( $fields, $country, $form ) {
199 if (isset($fields['_country'])) {
200 // some weird bug on the my account page
201 $form = '';
202 }
203
204 // Set required to true if country is NL
205 $required = ($country == 'NL')?true:false;
206
207 // Add Street name
208 $fields[$form.'_street_name'] = array(
209 'label' => __( 'Street name', 'woocommerce-postnl' ),
210 'placeholder' => __( 'Street name', 'woocommerce-postnl' ),
211 'class' => apply_filters( 'nl_custom_address_field_class', array( 'form-row-first' ), $form, 'street_name' ),
212 'required' => $required, // Only required for NL
213 );
214
215 // Add house number
216 $fields[$form.'_house_number'] = array(
217 'label' => __( 'Nr.', 'woocommerce-postnl' ),
218 // 'placeholder' => __( 'Nr.', 'woocommerce-postnl' ),
219 'class' => apply_filters( 'nl_custom_address_field_class', array( 'form-row-quart-first' ), $form, 'house_number' ),
220 'required' => $required, // Only required for NL
221 'type' => 'number',
222 );
223
224 // Add house number Suffix
225 $fields[$form.'_house_number_suffix'] = array(
226 'label' => __( 'Suffix', 'woocommerce-postnl' ),
227 // 'placeholder' => __( 'Suffix', 'woocommerce-postnl' ),
228 'class' => apply_filters( 'nl_custom_address_field_class', array( 'form-row-quart' ), $form, 'house_number_suffix' ),
229 'required' => false,
230 );
231
232 // Create new ordering for checkout fields
233 $order_keys = array (
234 $form.'_country',
235 $form.'_first_name',
236 $form.'_last_name',
237 $form.'_company',
238 $form.'_address_1',
239 $form.'_address_2',
240 $form.'_street_name',
241 $form.'_house_number',
242 $form.'_house_number_suffix',
243 $form.'_postcode',
244 $form.'_city',
245 $form.'_state',
246 );
247
248 if ($form == 'billing') {
249 array_push ($order_keys,
250 $form.'_email',
251 $form.'_phone'
252 );
253 }
254
255 $new_order = array();
256
257 // Create reordered array and fill with old array values
258 foreach ($order_keys as $key) {
259 $new_order[$key] = $fields[$key];
260 }
261
262 // Merge (&overwrite) field array
263 $fields = array_merge($new_order, $fields);
264
265 return $fields;
266 }
267
268 /**
269 * Hide state field for countries without states (backwards compatible fix for WooCommerce bug #4223)
270 * @param array $allowed_states states per country
271 * @return array
272 */
273 public function hide_states($allowed_states) {
274
275 $hidden_states = array(
276 'AF' => array(),
277 'AT' => array(),
278 'BE' => array(),
279 'BI' => array(),
280 'CZ' => array(),
281 'DE' => array(),
282 'DK' => array(),
283 'FI' => array(),
284 'FR' => array(),
285 'HU' => array(),
286 'IS' => array(),
287 'IL' => array(),
288 'KR' => array(),
289 'NL' => array(),
290 'NO' => array(),
291 'PL' => array(),
292 'PT' => array(),
293 'SG' => array(),
294 'SK' => array(),
295 'SI' => array(),
296 'LK' => array(),
297 'SE' => array(),
298 'VN' => array(),
299 );
300 $states = $hidden_states + $allowed_states;
301
302 return $states;
303 }
304
305 /**
306 * Localize checkout fields live
307 * @param array $locale_fields list of fields filtered by locale
308 * @return array $locale_fields with custom fields added
309 */
310 public function country_locale_field_selectors( $locale_fields ) {
311 $custom_locale_fields = array(
312 'street_name' => '#billing_street_name_field, #shipping_street_name_field',
313 'house_number' => '#billing_house_number_field, #shipping_house_number_field',
314 'house_number_suffix' => '#billing_house_number_suffix_field, #shipping_house_number_suffix_field',
315 );
316
317 $locale_fields = array_merge( $locale_fields, $custom_locale_fields );
318
319 return $locale_fields;
320 }
321
322 /**
323 * Make NL checkout fields hidden by default
324 * @param array $fields default checkout fields
325 * @return array $fields default + custom checkoud fields
326 */
327 public function default_address_fields( $fields ) {
328 $custom_fields = array(
329 'street_name' => array(
330 'hidden' => true,
331 'required' => false,
332 ),
333 'house_number' => array(
334 'hidden' => true,
335 'required' => false,
336 ),
337 'house_number_suffix' => array(
338 'hidden' => true,
339 'required' => false,
340 ),
341
342 );
343
344 $fields = array_merge( $fields,$custom_fields );
345
346 return $fields;
347 }
348
349 /**
350 * Load order custom data.
351 *
352 * @param array $data Default WC_Order data.
353 * @return array Custom WC_Order data.
354 */
355 public function load_order_data( $data ) {
356
357 // Billing
358 $data['billing_street_name'] = '';
359 $data['billing_house_number'] = '';
360 $data['billing_house_number_suffix'] = '';
361
362 // Shipping
363 $data['shipping_street_name'] = '';
364 $data['shipping_house_number'] = '';
365 $data['shipping_house_number_suffix'] = '';
366
367 return $data;
368 }
369
370 /**
371 * Custom billing admin edit fields.
372 *
373 * @param array $fields Default WC_Order data.
374 * @return array Custom WC_Order data.
375 */
376 public function admin_billing_fields( $fields ) {
377
378 $fields['street_name'] = array(
379 'label' => __( 'Street name', 'woocommerce-postnl' ),
380 'show' => true
381 );
382
383 $fields['house_number'] = array(
384 'label' => __( 'Number', 'woocommerce-postnl' ),
385 'show' => true
386 );
387
388 $fields['house_number_suffix'] = array(
389 'label' => __( 'Suffix', 'woocommerce-postnl' ),
390 'show' => true
391 );
392
393 return $fields;
394 }
395
396 /**
397 * Custom shipping admin edit fields.
398 *
399 * @param array $fields Default WC_Order data.
400 * @return array Custom WC_Order data.
401 */
402 public function admin_shipping_fields( $fields ) {
403
404 $fields['street_name'] = array(
405 'label' => __( 'Street name', 'woocommerce-postnl' ),
406 'show' => true
407 );
408
409 $fields['house_number'] = array(
410 'label' => __( 'Number', 'woocommerce-postnl' ),
411 'show' => true
412 );
413
414 $fields['house_number_suffix'] = array(
415 'label' => __( 'Suffix', 'woocommerce-postnl' ),
416 'show' => true
417 );
418
419 return $fields;
420 }
421
422 /**
423 * Custom user profile edit fields.
424 */
425 public function user_profile_fields ( $meta_fields ) {
426 $postnl_billing_fields = array(
427 'billing_street_name' => array(
428 'label' => __( 'Street', 'woocommerce-postnl' ),
429 'description' => ''
430 ),
431 'billing_house_number' => array(
432 'label' => __( 'Number', 'woocommerce-postnl' ),
433 'description' => ''
434 ),
435 'billing_house_number_suffix' => array(
436 'label' => __( 'Suffix', 'woocommerce-postnl' ),
437 'description' => ''
438 ),
439 );
440 $postnl_shipping_fields = array(
441 'shipping_street_name' => array(
442 'label' => __( 'Street', 'woocommerce-postnl' ),
443 'description' => ''
444 ),
445 'shipping_house_number' => array(
446 'label' => __( 'Number', 'woocommerce-postnl' ),
447 'description' => ''
448 ),
449 'shipping_house_number_suffix' => array(
450 'label' => __( 'Suffix', 'woocommerce-postnl' ),
451 'description' => ''
452 ),
453 );
454
455 // add postnl fields to billing section
456 $billing_fields = array_merge($meta_fields['billing']['fields'], $postnl_billing_fields);
457 $billing_fields = $this->array_move_keys( $billing_fields, array( 'billing_street_name', 'billing_house_number', 'billing_house_number_suffix' ), 'billing_address_2', 'after' );
458 $meta_fields['billing']['fields'] = $billing_fields;
459
460 // add postnl fields to shipping section
461 $shipping_fields = array_merge($meta_fields['shipping']['fields'], $postnl_shipping_fields);
462 $shipping_fields = $this->array_move_keys( $shipping_fields, array( 'shipping_street_name', 'shipping_house_number', 'shipping_house_number_suffix' ), 'shipping_address_2', 'after' );
463 $meta_fields['shipping']['fields'] = $shipping_fields;
464
465 return $meta_fields;
466 }
467
468 /**
469 * Add custom fields in customer details ajax.
470 * called when clicking the "Load billing/shipping address" button on Edit Order view
471 *
472 * @return void
473 */
474 public function customer_details_ajax( $customer_data ) {
475 $user_id = (int) trim( stripslashes( $_POST['user_id'] ) );
476 $type_to_load = esc_attr( trim( stripslashes( $_POST['type_to_load'] ) ) );
477
478 $custom_data = array(
479 $type_to_load . '_street_name' => get_user_meta( $user_id, $type_to_load . '_street_name', true ),
480 $type_to_load . '_house_number' => get_user_meta( $user_id, $type_to_load . '_house_number', true ),
481 $type_to_load . '_house_number_suffix' => get_user_meta( $user_id, $type_to_load . '_house_number_suffix', true ),
482 );
483
484 return array_merge( $customer_data, $custom_data );
485 }
486
487 /**
488 * Save custom fields from admin.
489 */
490 public function save_custom_fields( $post_id ) {
491 $post_type = get_post_type( $post_id );
492 if ( ( $post_type == 'shop_order' || $post_type == 'shop_order_refund' ) && !empty($_POST) ) {
493 $order = WCX::get_order( $post_id );
494 $addresses = array( 'billing', 'shipping' );
495 $address_fields = array( 'street_name', 'house_number', 'house_number_suffix' );
496 foreach ($addresses as $address) {
497 foreach ($address_fields as $address_field) {
498 if (isset($_POST["_{$address}_{$address_field}"])) {
499 WCX_Order::update_meta_data( $order, "_{$address}_{$address_field}", stripslashes( $_POST["_{$address}_{$address_field}"] ));
500 }
501 }
502 }
503 }
504 return;
505 }
506
507 /**
508 * Merge streetname, street number and street suffix into the default 'address_1' field
509 *
510 * @param string $order_id Order ID of checkout order.
511 * @return void
512 */
513 public function merge_street_number_suffix ( $order_id ) {
514 $order = WCX::get_order( $order_id );
515 // file_put_contents('postdata.txt', print_r($_POST,true)); // for debugging
516 if ( version_compare( WOOCOMMERCE_VERSION, '2.1', '<=' ) ) {
517 // old versions use 'shiptobilling'
518 $ship_to_different_address = isset($_POST['shiptobilling'])?false:true;
519 } else {
520 // WC2.1
521 $ship_to_different_address = isset($_POST['ship_to_different_address'])?true:false;
522 }
523
524 // check if country is NL
525 if ( $_POST['billing_country'] == 'NL' ) {
526 // concatenate street & house number & copy to 'billing_address_1'
527 $billing_house_number = $_POST['billing_house_number'] . (!empty($_POST['billing_house_number_suffix'])?'-' . $_POST['billing_house_number_suffix']:'');
528 $billing_address_1 = $_POST['billing_street_name'] . ' ' . $billing_house_number;
529 WCX_Order::set_address_prop( $order, 'address_1', 'billing', $billing_address_1 );
530
531 // check if 'ship to billing address' is checked
532 if ( $ship_to_different_address == false && $this->cart_needs_shipping_address() ) {
533 // use billing address
534 WCX_Order::set_address_prop( $order, 'address_1', 'shipping', $billing_address_1 );
535 }
536 }
537
538 if ( $_POST['shipping_country'] == 'NL' && $ship_to_different_address == true ) {
539 // concatenate street & house number & copy to 'shipping_address_1'
540 $shipping_house_number = $_POST['shipping_house_number'] . (!empty($_POST['shipping_house_number_suffix'])?'-' . $_POST['shipping_house_number_suffix']:'');
541 $shipping_address_1 = $_POST['shipping_street_name'] . ' ' . $shipping_house_number;
542 WCX_Order::set_address_prop( $order, 'address_1', 'shipping', $shipping_address_1 );
543 }
544 return;
545 }
546
547
548 /**
549 * validate NL postcodes
550 *
551 * @return bool $valid
552 */
553 public function validate_postcode( $valid, $postcode, $country ) {
554 if ($country == 'NL') {
555 $valid = (bool) preg_match( '/^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i', trim($postcode) );
556 }
557 return $valid;
558 }
559
560 /**
561 * Clean postcodes : remove space, dashes (& other non alfanumeric characters)
562 *
563 * @return $billing_postcode
564 * @return $shipping_postcode
565 */
566 public function clean_billing_postcode ( ) {
567 if ( $_POST['billing_country'] == 'NL' ) {
568 $billing_postcode = preg_replace('/[^a-zA-Z0-9]/', '', $_POST['billing_postcode']);
569 } else {
570 $billing_postcode = $_POST['billing_postcode'];
571 }
572 return $billing_postcode;
573 }
574 public function clean_shipping_postcode ( ) {
575 if ( $_POST['shipping_country'] == 'NL' ) {
576 $shipping_postcode = preg_replace('/[^a-zA-Z0-9]/', '', $_POST['shipping_postcode']);
577 } else {
578 $shipping_postcode = $_POST['shipping_postcode'];
579 }
580 return $shipping_postcode;
581 }
582
583 /**
584 * Remove placeholders from posted checkout data
585 * @param string $order_id order_id of the new order
586 * @param array $posted Array of posted form data
587 * @return void
588 */
589 public function remove_placeholders( $order_id, $posted ) {
590 $order = WCX::get_order( $order_id );
591 // get default address fields with their placeholders
592 $countries = new WC_Countries;
593 $fields = $countries->get_default_address_fields();
594
595 // define order_comments placeholder
596 $order_comments_placeholder = _x('Notes about your order, e.g. special notes for delivery.', 'placeholder', 'woocommerce');
597
598 // check if ship to billing is set
599 if ( version_compare( WOOCOMMERCE_VERSION, '2.1', '<=' ) ) {
600 // old versions use 'shiptobilling'
601 $ship_to_different_address = isset($_POST['shiptobilling'])?false:true;
602 } else {
603 // WC2.1
604 $ship_to_different_address = isset($_POST['ship_to_different_address'])?true:false;
605 }
606
607 // check the billing & shipping fields
608 $field_types = array('billing','shipping');
609 $check_fields = array('address_1','address_2','city','state','postcode');
610 foreach ($field_types as $field_type) {
611 foreach ($check_fields as $check_field) {
612 // file_put_contents(ABSPATH.'field_check.txt', $posted[$field_type.'_'.$check_field] .' || '. $fields[$check_field]['placeholder']."\n",FILE_APPEND);
613 if ( isset( $posted[$field_type.'_'.$check_field] ) && isset( $fields[$check_field]['placeholder'] ) && $posted[$field_type.'_'.$check_field] == $fields[$check_field]['placeholder'] ) {
614 WCX_Order::set_address_prop( $order, $check_field, $field_type, '' );
615
616 // also clear shipping field when ship_to_different_address is false
617 if ( $ship_to_different_address == false && $field_type == 'billing') {
618 WCX_Order::set_address_prop( $order, $check_field, 'shipping', '' );
619 }
620 }
621 }
622 }
623
624 // check the order comments field
625 if ($posted['order_comments'] == $order_comments_placeholder ) {
626 wp_update_post( array(
627 'ID' => $order_id,
628 'post_excerpt' => '',
629 )
630 );
631 }
632
633 return;
634 }
635
636 /**
637 * WooCommerce concatenates translations for required field notices that result in
638 * confusing messages, so we translate the full notice to prevent this
639 */
640 function required_field_notices( $notice, $field_label ) {
641 // concatenate translations
642 $billing_nr = sprintf( __( 'Billing %s', 'woocommerce' ), __( 'Nr.', 'woocommerce-postnl' ) );
643 $shipping_nr = sprintf( __( 'Shipping %s', 'woocommerce' ), __( 'Nr.', 'woocommerce-postnl' ) );
644 // not used:
645 // $billing_street = sprintf( __( 'Billing %s', 'woocommerce' ), __( 'Street name', 'woocommerce-postnl' ) );
646 // $shipping_street = sprintf( __( 'Shipping %s', 'woocommerce' ), __( 'Street name', 'woocommerce-postnl' ) );
647
648 switch ( $field_label ) {
649 case $billing_nr:
650 $notice = __( '<b>Billing Nr.</b> is a required field', 'woocommerce-postnl' );
651 break;
652 case $shipping_nr:
653 $notice = __( '<b>Shipping Nr.</b> is a required field', 'woocommerce-postnl' );
654 break;
655 default:
656 break;
657 }
658 return $notice;
659 }
660
661 /**
662 * Custom country address formats.
663 *
664 * @param array $formats Defaul formats.
665 *
666 * @return array New NL format.
667 */
668 public function localisation_address_formats( $formats ) {
669 // default = $postcode_before_city = "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}";
670 $formats['NL'] = "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}";
671 return $formats;
672 }
673
674 /**
675 * Custom country address format.
676 *
677 * @param array $replacements Default replacements.
678 * @param array $args Arguments to replace.
679 *
680 * @return array New replacements.
681 */
682 public function formatted_address_replacements( $replacements, $args ) {
683 extract( $args );
684
685 if (!empty($street_name) && $country == 'NL') {
686 $replacements['{address_1}'] = $street_name.' '.$house_number.$house_number_suffix;
687 }
688
689 return $replacements;
690 }
691
692 /**
693 * Custom order formatted billing address.
694 *
695 * @param array $address Default address.
696 * @param object $order Order data.
697 *
698 * @return array New address format.
699 */
700 public function order_formatted_billing_address( $address, $order ) {
701 $address['street_name'] = WCX_Order::get_meta( $order, '_billing_street_name', true, 'view' );
702 $address['house_number'] = WCX_Order::get_meta( $order, '_billing_house_number', true, 'view' );
703 $address['house_number_suffix'] = WCX_Order::get_meta( $order, '_billing_house_number_suffix', true, 'view' );
704 $address['house_number_suffix'] = !empty($address['house_number_suffix'])?'-'.$address['house_number_suffix']:'';
705
706 return $address;
707 }
708
709 /**
710 * Custom order formatted shipping address.
711 *
712 * @param array $address Default address.
713 * @param object $order Order data.
714 *
715 * @return array New address format.
716 */
717 public function order_formatted_shipping_address( $address, $order ) {
718 $address['street_name'] = WCX_Order::get_meta( $order, '_shipping_street_name', true, 'view' );
719 $address['house_number'] = WCX_Order::get_meta( $order, '_shipping_house_number', true, 'view' );
720 $address['house_number_suffix'] = WCX_Order::get_meta( $order, '_shipping_house_number_suffix', true, 'view' );
721 $address['house_number_suffix'] = !empty($address['house_number_suffix'])?'-'.$address['house_number_suffix']:'';
722
723 return $address;
724 }
725
726 /**
727 * Custom user column billing address information.
728 *
729 * @param array $address Default address.
730 * @param int $user_id User id.
731 *
732 * @return array New address format.
733 */
734 public function user_column_billing_address( $address, $user_id ) {
735 $address['street_name'] = get_user_meta( $user_id, 'billing_street_name', true );
736 $address['house_number'] = get_user_meta( $user_id, 'billing_house_number', true );
737 $address['house_number_suffix'] = (get_user_meta( $user_id, 'billing_house_number_suffix', true ))?'-'.get_user_meta( $user_id, 'billing_house_number_suffix', true ):'';
738
739 return $address;
740 }
741
742 /**
743 * Custom user column shipping address information.
744 *
745 * @param array $address Default address.
746 * @param int $user_id User id.
747 *
748 * @return array New address format.
749 */
750 public function user_column_shipping_address( $address, $user_id ) {
751 $address['street_name'] = get_user_meta( $user_id, 'shipping_street_name', true );
752 $address['house_number'] = get_user_meta( $user_id, 'shipping_house_number', true );
753 $address['house_number_suffix'] = (get_user_meta( $user_id, 'shipping_house_number_suffix', true ))?'-'.get_user_meta( $user_id, 'shipping_house_number_suffix', true ):'';
754
755 return $address;
756 }
757
758 /**
759 * Custom my address formatted address.
760 *
761 * @param array $address Default address.
762 * @param int $customer_id Customer ID.
763 * @param string $name Field name (billing or shipping).
764 *
765 * @return array New address format.
766 */
767 public function my_account_my_address_formatted_address( $address, $customer_id, $name ) {
768 $address['street_name'] = get_user_meta( $customer_id, $name . '_street_name', true );
769 $address['house_number'] = get_user_meta( $customer_id, $name . '_house_number', true );
770 $address['house_number_suffix'] = (get_user_meta( $customer_id, $name . '_house_number_suffix', true ))?'-'.get_user_meta( $customer_id, $name . '_house_number_suffix', true ):'';
771
772 return $address;
773 }
774
775 /**
776 * Get a posted address field after sanitization and validation.
777 *
778 * @param string $key
779 * @param string $type billing for shipping
780 *
781 * @return string
782 */
783 public function get_posted_address_data( $key, $posted, $type = 'billing' ) {
784 if ( 'billing' === $type || ( !$posted['ship_to_different_address'] && $this->cart_needs_shipping_address() ) ) {
785 $return = isset( $posted[ 'billing_' . $key ] ) ? $posted[ 'billing_' . $key ] : '';
786 } elseif ( 'shipping' === $type && !$this->cart_needs_shipping_address() ) {
787 $return = '';
788 } else {
789 $return = isset( $posted[ 'shipping_' . $key ] ) ? $posted[ 'shipping_' . $key ] : '';
790 }
791
792 return $return;
793 }
794
795 public function cart_needs_shipping_address() {
796 if ( is_object( WC()->cart ) && method_exists( WC()->cart, 'needs_shipping_address' ) && function_exists('wc_ship_to_billing_address_only') ) {
797 if ( WC()->cart->needs_shipping_address() || wc_ship_to_billing_address_only() ) {
798 return true;
799 } else {
800 return false;
801 }
802 } else {
803 return true;
804 }
805 }
806
807 /**
808 * Save order data.
809 *
810 * @param int $order_id
811 * @param array $posted
812 *
813 * @return void
814 */
815 public function save_order_data( $order_id, $posted ) {
816 $order = WCX::get_order( $order_id );
817 // Billing.
818 WCX_Order::update_meta_data( $order, '_billing_street_name', $this->get_posted_address_data( 'street_name', $posted ) );
819 WCX_Order::update_meta_data( $order, '_billing_house_number', $this->get_posted_address_data( 'house_number', $posted ) );
820 WCX_Order::update_meta_data( $order, '_billing_house_number_suffix', $this->get_posted_address_data( 'house_number_suffix', $posted ) );
821
822 // Shipping.
823 WCX_Order::update_meta_data( $order, '_shipping_street_name', $this->get_posted_address_data( 'street_name', $posted, 'shipping' ) );
824 WCX_Order::update_meta_data( $order, '_shipping_house_number', $this->get_posted_address_data( 'house_number', $posted, 'shipping' ) );
825 WCX_Order::update_meta_data( $order, '_shipping_house_number_suffix', $this->get_posted_address_data( 'house_number_suffix', $posted, 'shipping' ) );
826 }
827
828 /**
829 * Helper function to move array elements (one or more) to a position before a specific key
830 * @param array $array Main array to modify
831 * @param mixed $keys Single key or array of keys of element(s) to move
832 * @param string $reference_key key to put elements before or after
833 * @param string $postion before or after
834 * @return array reordered array
835 */
836 public function array_move_keys ( $array, $keys, $reference_key, $position = 'before' ) {
837 // cast $key as array
838 $keys = (array) $keys;
839
840 if (!isset($array[$reference_key])) {
841 return $array;
842 }
843
844 $move = array();
845 foreach ($keys as $key) {
846 if (!isset($array[$key])) {
847 continue;
848 }
849 $move[$key] = $array[$key];
850 unset ($array[$key]);
851 }
852
853 if ($position == 'before') {
854 $move_to_pos = array_search($reference_key, array_keys($array));
855 } else { // after
856 $move_to_pos = array_search($reference_key, array_keys($array)) + 1;
857 }
858
859 $new_array =
860 array_slice($array, 0, $move_to_pos, true)
861 + $move
862 + array_slice($array, $move_to_pos, NULL, true);
863
864 return $new_array;
865 }
866 }
867
868 endif; // class_exists
869
870 return new WC_NLPostcode_Fields();