PluginProbe
PostNL for WooCommerce / 5.9.12
PostNL for WooCommerce v5.9.12
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 / src / Address_Utils.php

Address_Utils.php in PostNL for WooCommerce 5.9.12, at src/Address_Utils.php

136 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Address Utils file.
4 *
5 * @package PostNLWooCommerce
6 */
7
8 namespace PostNLWooCommerce;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Class AddressUtils.
16 *
17 * @package PostNLWooCommerce
18 */
19 class Address_Utils {
20 /**
21 * Get Cart Billing country.
22 *
23 * @return string|null
24 */
25 public static function get_customer_billing_country() {
26 return WC()->customer->get_billing_country();
27 }
28
29 /**
30 * Get Cart Shipping country.
31 *
32 * @return string|null
33 */
34 public static function get_customer_shipping_country() {
35 return WC()->customer->get_shipping_country();
36 }
37
38 /**
39 * Set shipping address based on post data from checkout page.
40 *
41 * @param array $post_data Post data from checkout page.
42 *
43 * @return array
44 */
45 public static function set_post_data_address( $post_data ) {
46 if ( empty( $post_data['ship_to_different_address'] ) ) {
47 $post_data['shipping_first_name'] = $post_data['billing_first_name'] ?? '';
48 $post_data['shipping_last_name'] = $post_data['billing_last_name'] ?? '';
49 $post_data['shipping_company'] = $post_data['billing_company'] ?? '';
50 $post_data['shipping_address_1'] = $post_data['billing_address_1'] ?? '';
51 $post_data['shipping_address_2'] = $post_data['billing_address_2'] ?? '';
52 $post_data['shipping_city'] = $post_data['billing_city'] ?? '';
53 $post_data['shipping_state'] = $post_data['billing_state'] ?? '';
54 $post_data['shipping_country'] = $post_data['billing_country'] ?? '';
55 $post_data['shipping_postcode'] = $post_data['billing_postcode'] ?? '';
56 }
57
58 return self::set_address_house_number( $post_data );
59 }
60
61 /**
62 * Get house number from address.
63 */
64 public static function set_address_house_number( $post_data ) {
65 // Return house number if posted
66 if ( isset( $post_data['billing_house_number'] ) && empty( $post_data['ship_to_different_address'] ) ) {
67 // Set shipping house number
68 $post_data['shipping_house_number'] = $post_data['billing_house_number'];
69
70 return $post_data;
71 } elseif ( isset( $post_data['shipping_house_number'] ) ) {
72 // Nothing to do
73 return $post_data;
74 }
75
76 // Split Address 1 then set HouseNumber & HouseNumber Extension
77 return self::split_address( $post_data, true );
78 }
79
80 /**
81 * Split address into street and house number.
82 *
83 * @param array $post_data
84 * @param bool $is_checkout_data
85 *
86 * @return array
87 */
88 public static function split_address( array $post_data, bool $is_checkout_data = false ): array {
89 // If its checkout posted data, add shipping_ prefix to array keys.
90 $address_type = '';
91 if ( $is_checkout_data ) {
92 $address_type = 'shipping_';
93 }
94
95 // If its contain house number, nothing to do.
96 if ( ! empty( $post_data[ $address_type . 'house_number' ] ) ) {
97 return $post_data;
98 }
99
100 // Break address into pieces by spaces.
101 $address_exploded = explode( ' ', $post_data[ $address_type . 'address_1' ] );
102 $address_has_number = false;
103
104 foreach ( $address_exploded as $address_key => $address_value ) {
105 // Number count in address 1.
106 if ( is_numeric( $address_value ) ) {
107 $set_key = $address_key;
108 $address_has_number = true;
109 }
110 }
111
112 if ( ! $address_has_number ) {
113 return $post_data;
114 }
115
116 // The number is the first part of address 1.
117 if ( 0 === $set_key ) {
118 // Set "house_number" first, as first part.
119 $post_data[ $address_type . 'house_number' ] = implode( ' ', array_slice( $address_exploded, 0, 1 ) );
120
121 // Remove "house_number" from "address_1".
122 $post_data[ $address_type . 'address_1' ] = implode( ' ', array_slice( $address_exploded, 1 ) );
123 } else {
124 if ( empty( $post_data[ $address_type . 'address_2' ] ) ) {
125 // Set "address_2" to be house number extension.
126 $post_data[ $address_type . 'address_2' ] = implode( ' ', array_slice( $address_exploded, $set_key + 1, count( $address_exploded ) ) );
127 }
128
129 $post_data[ $address_type . 'house_number' ] = $address_exploded[ $set_key ];
130 $post_data[ $address_type . 'address_1' ] = implode( ' ', array_slice( $address_exploded, 0, $set_key ) );
131 }
132
133 return $post_data;
134 }
135 }
136