WC_NLPostcode_Fields

The WooCommerce Flespakket WC NLPostcode Fields class.

Defined (1)

The class is defined in the following location(s).

/includes/wcflespakket-nlpostcode-fields.php  
  1. class WC_NLPostcode_Fields { 
  2.  
  3. /** 
  4. * Construct. 
  5. */ 
  6.  
  7. public function __construct() { 
  8. // Load styles & scripts 
  9. add_action( 'wp_enqueue_scripts', array( &$this, 'add_styles_scripts' ) ); 
  10.  
  11. // Add street name & house number checkout fields. 
  12. if ( version_compare( WOOCOMMERCE_VERSION, '2.0' ) >= 0 ) { 
  13. // WC 2.0 or newer is used, the filter got a $coutry parameter, yay! 
  14. add_filter( 'woocommerce_billing_fields', array( &$this, 'nl_billing_fields' ), 10, 2 ); 
  15. add_filter( 'woocommerce_shipping_fields', array( &$this, 'nl_shipping_fields' ), 10, 2 ); 
  16. } else { 
  17. // Backwards compatibility 
  18. add_filter( 'woocommerce_billing_fields', array( &$this, 'nl_billing_fields' ) ); 
  19. add_filter( 'woocommerce_shipping_fields', array( &$this, 'nl_shipping_fields' ) ); 
  20.  
  21.  
  22. // Hide state field for countries without states (backwards compatible fix for bug #4223) 
  23. add_filter( 'woocommerce_countries_allowed_country_states', array( &$this, 'hide_states' ) ); 
  24.  
  25. // Localize checkout fields (limit custom checkout fields to NL) 
  26. add_filter( 'woocommerce_country_locale_field_selectors', array( &$this, 'country_locale_field_selectors' ) ); 
  27. add_filter( 'woocommerce_default_address_fields', array( &$this, 'default_address_fields' ) ); 
  28. add_filter( 'woocommerce_get_country_locale', array( &$this, 'woocommerce_locale_nl' ), 1, 1); 
  29.  
  30. // Load custom order data. 
  31. add_filter( 'woocommerce_load_order_data', array( &$this, 'load_order_data' ) ); 
  32.  
  33. // Custom shop_order details. 
  34. add_filter( 'woocommerce_admin_billing_fields', array( &$this, 'admin_billing_fields' ) ); 
  35. add_filter( 'woocommerce_admin_shipping_fields', array( &$this, 'admin_shipping_fields' ) ); 
  36. add_filter( 'woocommerce_found_customer_details', array( $this, 'customer_details_ajax' ) ); 
  37. add_action( 'save_post', array( &$this, 'save_custom_fields' ) ); 
  38.  
  39. // Processing checkout 
  40. add_action('woocommerce_checkout_update_order_meta', array( &$this, 'merge_street_number_suffix' ), 20, 2 ); 
  41. add_filter('woocommerce_process_checkout_field_billing_postcode', array( &$this, 'clean_billing_postcode' ) ); 
  42. add_filter('woocommerce_process_checkout_field_shipping_postcode', array( &$this, 'clean_shipping_postcode' ) ); 
  43.  
  44. // Save the order data in WooCommerce 2.2 or later. 
  45. if ( version_compare( WOOCOMMERCE_VERSION, '2.2' ) >= 0 ) { 
  46. add_action( 'woocommerce_checkout_update_order_meta', array( &$this, 'save_order_data' ), 10, 2 ); 
  47.  
  48. // Remove placeholder values (IE8 & 9) 
  49. add_action('woocommerce_checkout_update_order_meta', array( &$this, 'remove_placeholders' ), 10, 2 ); 
  50.  
  51. $this->load_woocommerce_filters(); 
  52.  
  53. public function load_woocommerce_filters() { 
  54. // Custom address format. 
  55. if ( version_compare( WOOCOMMERCE_VERSION, '2.0.6', '>=' ) ) { 
  56. add_filter( 'woocommerce_localisation_address_formats', array( $this, 'localisation_address_formats' ) ); 
  57. add_filter( 'woocommerce_formatted_address_replacements', array( $this, 'formatted_address_replacements' ), 1, 2 ); 
  58. add_filter( 'woocommerce_order_formatted_billing_address', array( $this, 'order_formatted_billing_address' ), 1, 2 ); 
  59. add_filter( 'woocommerce_order_formatted_shipping_address', array( $this, 'order_formatted_shipping_address' ), 1, 2 ); 
  60. add_filter( 'woocommerce_user_column_billing_address', array( $this, 'user_column_billing_address' ), 1, 2 ); 
  61. add_filter( 'woocommerce_user_column_shipping_address', array( $this, 'user_column_shipping_address' ), 1, 2 ); 
  62. add_filter( 'woocommerce_my_account_my_address_formatted_address', array( $this, 'my_account_my_address_formatted_address' ), 1, 3 ); 
  63.  
  64.  
  65. /** 
  66. * Load styles & scripts. 
  67. */ 
  68. public function add_styles_scripts() { 
  69. if ( is_checkout() || is_account_page() ) { 
  70. if ( version_compare( WOOCOMMERCE_VERSION, '2.1', '<=' ) ) { 
  71. // Backwards compatibility for https://github.com/woothemes/woocommerce/issues/4239 
  72. wp_register_script( 'nl-checkout', (dirname(plugin_dir_url(__FILE__)) . '/js/nl-checkout.js'), array( 'wc-checkout' ) ); 
  73. wp_enqueue_script( 'nl-checkout' ); 
  74.  
  75. if ( is_account_page() ) { 
  76. // Disable regular address fields for NL on account page - Fixed in WC 2.1 but not on init... 
  77. wp_register_script( 'nl-account-page', (dirname(plugin_dir_url(__FILE__)) . '/js/nl-account-page.js'), array( 'jquery' ) ); 
  78. wp_enqueue_script( 'nl-account-page' ); 
  79.  
  80. wp_enqueue_style( 'nl-checkout', (dirname(plugin_dir_url(__FILE__)) . '/css/nl-checkout.css') ); 
  81.  
  82.  
  83. /** 
  84. * Hide default Dutch address fields 
  85. * @param array $locale woocommerce country locale field settings 
  86. * @return array $locale 
  87. */ 
  88. public function woocommerce_locale_nl( $locale ) { 
  89. $locale['NL']['address_1'] = array( 
  90. 'required' => false,  
  91. 'hidden' => true,  
  92. ); 
  93.  
  94. $locale['NL']['address_2'] = array( 
  95. 'hidden' => true,  
  96. ); 
  97.  
  98. $locale['NL']['state'] = array( 
  99. 'hidden' => true,  
  100. 'required' => false,  
  101. ); 
  102.  
  103. $locale['NL']['street_name'] = array( 
  104. 'required' => true,  
  105. 'hidden' => false,  
  106. ); 
  107.  
  108. $locale['NL']['house_number'] = array( 
  109. 'required' => true,  
  110. 'hidden' => false,  
  111. ); 
  112.  
  113. $locale['NL']['house_number_suffix'] = array( 
  114. 'required' => false,  
  115. 'hidden' => false,  
  116. ); 
  117.  
  118. return $locale; 
  119.  
  120. public function nl_billing_fields( $fields, $country = '' ) { 
  121. return $this->nl_checkout_fields( $fields, $country, 'billing'); 
  122.  
  123. public function nl_shipping_fields( $fields, $country = '' ) { 
  124. return $this->nl_checkout_fields( $fields, $country, 'shipping'); 
  125.  
  126. /** 
  127. * New checkout billing/shipping fields 
  128. * @param array $fields Default fields. 
  129. * @return array $fields New fields. 
  130. */ 
  131. public function nl_checkout_fields( $fields, $country, $form ) { 
  132. if (isset($fields['_country'])) { 
  133. // some weird bug on the my account page 
  134. $form = ''; 
  135.  
  136. // Set required to true if country is NL 
  137. $required = ($country == 'NL')?true:false; 
  138.  
  139. // Add Street name 
  140. $fields[$form.'_street_name'] = array( 
  141. 'label' => __( 'Street name', 'wcflespakket' ),  
  142. 'placeholder' => __( 'Street name', 'wcflespakket' ),  
  143. 'class' => array( 'form-row-first' ),  
  144. 'required' => $required, // Only required for NL 
  145. ); 
  146.  
  147. // Add house number 
  148. $fields[$form.'_house_number'] = array( 
  149. 'label' => __( 'Nr.', 'wcflespakket' ),  
  150. // 'placeholder' => __( 'Nr.', 'wcflespakket' ),  
  151. 'class' => array( 'form-row-quart-first' ),  
  152. 'required' => $required, // Only required for NL 
  153. ); 
  154.  
  155. // Add house number Suffix 
  156. $fields[$form.'_house_number_suffix'] = array( 
  157. 'label' => __( 'Suffix', 'wcflespakket' ),  
  158. // 'placeholder' => __( 'Suffix', 'wcflespakket' ),  
  159. 'class' => array( 'form-row-quart' ),  
  160. 'required' => false,  
  161. ); 
  162.  
  163. // Create new ordering for checkout fields 
  164. $order_keys = array ( 
  165. $form.'_country',  
  166. $form.'_first_name',  
  167. $form.'_last_name',  
  168. $form.'_company',  
  169. $form.'_address_1',  
  170. $form.'_address_2',  
  171. $form.'_street_name',  
  172. $form.'_house_number',  
  173. $form.'_house_number_suffix',  
  174. $form.'_postcode',  
  175. $form.'_city',  
  176. $form.'_state',  
  177. ); 
  178.  
  179. if ($form == 'billing') { 
  180. array_push ($order_keys,  
  181. $form.'_email',  
  182. $form.'_phone' 
  183. ); 
  184.  
  185. $new_order = array(); 
  186.  
  187. // Create reordered array and fill with old array values 
  188. foreach ($order_keys as $key) { 
  189. $new_order[$key] = $fields[$key]; 
  190.  
  191. // Merge (&overwrite) field array 
  192. $fields = array_merge($new_order, $fields); 
  193.  
  194. return $fields; 
  195.  
  196. /** 
  197. * Hide state field for countries without states (backwards compatible fix for WooCommerce bug #4223) 
  198. * @param array $allowed_states states per country 
  199. * @return array  
  200. */ 
  201. public function hide_states($allowed_states) { 
  202.  
  203. $hidden_states = array( 
  204. 'AF' => array(),  
  205. 'AT' => array(),  
  206. 'BE' => array(),  
  207. 'BI' => array(),  
  208. 'CZ' => array(),  
  209. 'DE' => array(),  
  210. 'DK' => array(),  
  211. 'FI' => array(),  
  212. 'FR' => array(),  
  213. 'HU' => array(),  
  214. 'IS' => array(),  
  215. 'IL' => array(),  
  216. 'KR' => array(),  
  217. 'NL' => array(),  
  218. 'NO' => array(),  
  219. 'PL' => array(),  
  220. 'PT' => array(),  
  221. 'SG' => array(),  
  222. 'SK' => array(),  
  223. 'SI' => array(),  
  224. 'LK' => array(),  
  225. 'SE' => array(),  
  226. 'VN' => array(),  
  227. ); 
  228. $states = $hidden_states + $allowed_states; 
  229.  
  230. return $states; 
  231.  
  232. /** 
  233. * Localize checkout fields live 
  234. * @param array $locale_fields list of fields filtered by locale 
  235. * @return array $locale_fields with custom fields added 
  236. */ 
  237. public function country_locale_field_selectors( $locale_fields ) { 
  238. $custom_locale_fields = array( 
  239. 'street_name' => '#billing_street_name_field, #shipping_street_name_field',  
  240. 'house_number' => '#billing_house_number_field, #shipping_house_number_field',  
  241. 'house_number_suffix' => '#billing_house_number_suffix_field, #shipping_house_number_suffix_field',  
  242. ); 
  243.  
  244. $locale_fields = array_merge( $locale_fields, $custom_locale_fields ); 
  245.  
  246. return $locale_fields; 
  247.  
  248. /** 
  249. * Make NL checkout fields hidden by default 
  250. * @param array $fields default checkout fields 
  251. * @return array $fields default + custom checkoud fields 
  252. */ 
  253. public function default_address_fields( $fields ) { 
  254. $custom_fields = array( 
  255. 'street_name' => array( 
  256. 'hidden' => true,  
  257. 'required' => false,  
  258. ),  
  259. 'house_number' => array( 
  260. 'hidden' => true,  
  261. 'required' => false,  
  262. ),  
  263. 'house_number_suffix' => array( 
  264. 'hidden' => true,  
  265. 'required' => false,  
  266. ),  
  267.  
  268. ); 
  269.  
  270. $fields = array_merge( $fields, $custom_fields ); 
  271.  
  272. return $fields; 
  273.  
  274. /** 
  275. * Load order custom data. 
  276. * @param array $data Default WC_Order data. 
  277. * @return array Custom WC_Order data. 
  278. */ 
  279. public function load_order_data( $data ) { 
  280.  
  281. // Billing 
  282. $data['billing_street_name'] = ''; 
  283. $data['billing_house_number'] = ''; 
  284. $data['billing_house_number_suffix'] = '';  
  285.  
  286. // Shipping 
  287. $data['shipping_street_name'] = ''; 
  288. $data['shipping_house_number'] = ''; 
  289. $data['shipping_house_number_suffix'] = ''; 
  290.  
  291. return $data; 
  292.  
  293. /** 
  294. * Custom billing admin edit fields. 
  295. * @param array $fields Default WC_Order data. 
  296. * @return array Custom WC_Order data. 
  297. */ 
  298. public function admin_billing_fields( $fields ) { 
  299.  
  300. $fields['street_name'] = array( 
  301. 'label' => __( 'Street name', 'wcflespakket' ),  
  302. 'show' => true 
  303. ); 
  304.  
  305. $fields['house_number'] = array( 
  306. 'label' => __( 'Number', 'wcflespakket' ),  
  307. 'show' => true 
  308. ); 
  309.  
  310. $fields['house_number_suffix'] = array( 
  311. 'label' => __( 'Suffix', 'wcflespakket' ),  
  312. 'show' => true 
  313. ); 
  314.  
  315. return $fields; 
  316.  
  317. /** 
  318. * Custom shipping admin edit fields. 
  319. * @param array $fields Default WC_Order data. 
  320. * @return array Custom WC_Order data. 
  321. */ 
  322. public function admin_shipping_fields( $fields ) { 
  323.  
  324. $fields['street_name'] = array( 
  325. 'label' => __( 'Street name', 'wcflespakket' ),  
  326. 'show' => true 
  327. ); 
  328.  
  329. $fields['house_number'] = array( 
  330. 'label' => __( 'Number', 'wcflespakket' ),  
  331. 'show' => true 
  332. ); 
  333.  
  334. $fields['house_number_suffix'] = array( 
  335. 'label' => __( 'Suffix', 'wcflespakket' ),  
  336. 'show' => true 
  337. ); 
  338.  
  339. return $fields; 
  340.  
  341. /** 
  342. * Add custom fields in customer details ajax. 
  343. * called when clicking the "Load billing/shipping address" button on Edit Order view 
  344. * @return void 
  345. */ 
  346. public function customer_details_ajax( $customer_data ) { 
  347. $user_id = (int) trim( stripslashes( $_POST['user_id'] ) ); 
  348. $type_to_load = esc_attr( trim( stripslashes( $_POST['type_to_load'] ) ) ); 
  349.  
  350. $custom_data = array( 
  351. $type_to_load . '_street_name' => get_user_meta( $user_id, $type_to_load . '_street_name', true ),  
  352. $type_to_load . '_house_number' => get_user_meta( $user_id, $type_to_load . '_house_number', true ),  
  353. $type_to_load . '_house_number_suffix' => get_user_meta( $user_id, $type_to_load . '_house_number_suffix', true ),  
  354. ); 
  355.  
  356. return array_merge( $customer_data, $custom_data ); 
  357.  
  358. /** 
  359. * Save custom fields from admin. 
  360. */ 
  361. public function save_custom_fields($post_id) { 
  362. global $post_type; 
  363. if( $post_type == 'shop_order' && !empty($_POST) ) { 
  364. update_post_meta( $post_id, '_billing_street_name', stripslashes( $_POST['_billing_street_name'] )); 
  365. update_post_meta( $post_id, '_billing_house_number', stripslashes( $_POST['_billing_house_number'] )); 
  366. update_post_meta( $post_id, '_billing_house_number_suffix', stripslashes( $_POST['_billing_house_number_suffix'] )); 
  367.  
  368. update_post_meta( $post_id, '_shipping_street_name', stripslashes( $_POST['_shipping_street_name'] )); 
  369. update_post_meta( $post_id, '_shipping_house_number', stripslashes( $_POST['_shipping_house_number'] )); 
  370. update_post_meta( $post_id, '_shipping_house_number_suffix', stripslashes( $_POST['_shipping_house_number_suffix'] )); 
  371. return; 
  372.  
  373. /** 
  374. * Merge streetname, street number and street suffix into the default 'address_1' field 
  375. * @param string $order_id Order ID of checkout order. 
  376. * @return void 
  377. */ 
  378. public function merge_street_number_suffix ( $order_id ) { 
  379. // file_put_contents('postdata.txt', print_r($_POST, true)); // for debugging 
  380. if ( version_compare( WOOCOMMERCE_VERSION, '2.1', '<=' ) ) { 
  381. // old versions use 'shiptobilling' 
  382. $ship_to_different_address = isset($_POST['shiptobilling'])?false:true; 
  383. } else { 
  384. // WC2.1 
  385. $ship_to_different_address = isset($_POST['ship_to_different_address'])?true:false; 
  386.  
  387. // check if country is NL 
  388. if ( $_POST['billing_country'] == 'NL' ) { 
  389. // concatenate street & house number & copy to 'billing_address_1' 
  390. $billing_house_number = $_POST['billing_house_number'] . (!empty($_POST['billing_house_number_suffix'])?'-' . $_POST['billing_house_number_suffix']:''); 
  391. $billing_address_1 = $_POST['billing_street_name'] . ' ' . $billing_house_number; 
  392. update_post_meta( $order_id, '_billing_address_1', $billing_address_1 ); 
  393.  
  394. // check if 'ship to billing address' is checked 
  395. if ( $ship_to_different_address == false ) { 
  396. // use billing address 
  397. update_post_meta( $order_id, '_shipping_address_1', $billing_address_1 ); 
  398.  
  399. if ( $_POST['shipping_country'] == 'NL' && $ship_to_different_address == true) { 
  400. // concatenate street & house number & copy to 'shipping_address_1' 
  401. $shipping_house_number = $_POST['shipping_house_number'] . (!empty($_POST['shipping_house_number_suffix'])?'-' . $_POST['shipping_house_number_suffix']:''); 
  402. $shipping_address_1 = $_POST['shipping_street_name'] . ' ' . $shipping_house_number; 
  403. update_post_meta( $order_id, '_shipping_address_1', $shipping_address_1 ); 
  404. return; 
  405.  
  406. /** 
  407. * Clean postcodes : remove space, dashes (& other non alfanumeric characters) 
  408. * @return $billing_postcode 
  409. * @return $shipping_postcode 
  410. */ 
  411. public function clean_billing_postcode ( ) { 
  412. $billing_postcode = preg_replace('/[^a-zA-Z0-9]/', '', $_POST['billing_postcode']);  
  413. return $billing_postcode; 
  414. public function clean_shipping_postcode ( ) { 
  415. $shipping_postcode = preg_replace('/[^a-zA-Z0-9]/', '', $_POST['shipping_postcode']);  
  416. return $shipping_postcode; 
  417.  
  418. /** 
  419. * Remove placeholders from posted checkout data 
  420. * @param string $order_id order_id of the new order 
  421. * @param array $posted Array of posted form data 
  422. * @return void 
  423. */ 
  424. public function remove_placeholders( $order_id, $posted ) { 
  425. // get default address fields with their placeholders 
  426. $countries = new WC_Countries; 
  427. $fields = $countries->get_default_address_fields(); 
  428.  
  429. // define order_comments placeholder 
  430. $order_comments_placeholder = _x('Notes about your order, e.g. special notes for delivery.', 'placeholder', 'woocommerce'); 
  431.  
  432. // check if ship to billing is set 
  433. if ( version_compare( WOOCOMMERCE_VERSION, '2.1', '<=' ) ) { 
  434. // old versions use 'shiptobilling' 
  435. $ship_to_different_address = isset($_POST['shiptobilling'])?false:true; 
  436. } else { 
  437. // WC2.1 
  438. $ship_to_different_address = isset($_POST['ship_to_different_address'])?true:false; 
  439.  
  440. // check the billing & shipping fields 
  441. $field_types = array('billing', 'shipping'); 
  442. $check_fields = array('address_1', 'address_2', 'city', 'state', 'postcode'); 
  443. foreach ($field_types as $field_type) { 
  444. foreach ($check_fields as $check_field) { 
  445. // file_put_contents(ABSPATH.'field_check.txt', $posted[$field_type.'_'.$check_field] .' || '. $fields[$check_field]['placeholder']."\n", FILE_APPEND); 
  446. if ( isset( $posted[$field_type.'_'.$check_field] ) && $posted[$field_type.'_'.$check_field] == $fields[$check_field]['placeholder'] ) { 
  447. update_post_meta( $order_id, '_'.$field_type.'_'.$check_field, '' ); 
  448.  
  449. // also clear shipping field when ship_to_different_address is false 
  450. if ( $ship_to_different_address == false && $field_type == 'billing') { 
  451. update_post_meta( $order_id, '_shipping_'.$check_field, '' ); 
  452.  
  453. // check the order comments field  
  454. if ($posted['order_comments'] == $order_comments_placeholder ) { 
  455. wp_update_post( array( 
  456. 'ID' => $order_id,  
  457. 'post_excerpt' => '',  
  458. );  
  459.  
  460. return; 
  461.  
  462. /** 
  463. * Custom country address formats. 
  464. * @param array $formats Defaul formats. 
  465. * @return array New NL format. 
  466. */ 
  467. public function localisation_address_formats( $formats ) { 
  468. // default = $postcode_before_city = "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}"; 
  469. $formats['NL'] = "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}"; 
  470. return $formats; 
  471.  
  472. /** 
  473. * Custom country address format. 
  474. * @param array $replacements Default replacements. 
  475. * @param array $args Arguments to replace. 
  476. * @return array New replacements. 
  477. */ 
  478. public function formatted_address_replacements( $replacements, $args ) { 
  479. extract( $args ); 
  480.  
  481. if (!empty($street_name)) { 
  482. $replacements['{address_1}'] = $street_name.' '.$house_number.$house_number_suffix; 
  483.  
  484. return $replacements; 
  485.  
  486. /** 
  487. * Custom order formatted billing address. 
  488. * @param array $address Default address. 
  489. * @param object $order Order data. 
  490. * @return array New address format. 
  491. */ 
  492. public function order_formatted_billing_address( $address, $order ) { 
  493. $address['street_name'] = $order->billing_street_name; 
  494. $address['house_number'] = $order->billing_house_number; 
  495. $address['house_number_suffix'] = !empty($order->billing_house_number_suffix)?'-'.$order->billing_house_number_suffix:''; 
  496.  
  497. return $address; 
  498.  
  499. /** 
  500. * Custom order formatted shipping address. 
  501. * @param array $address Default address. 
  502. * @param object $order Order data. 
  503. * @return array New address format. 
  504. */ 
  505. public function order_formatted_shipping_address( $address, $order ) { 
  506. $address['street_name'] = $order->shipping_street_name; 
  507. $address['house_number'] = $order->shipping_house_number; 
  508. $address['house_number_suffix'] = !empty($order->shipping_house_number_suffix)?'-'.$order->shipping_house_number_suffix:''; 
  509.  
  510. return $address; 
  511.  
  512. /** 
  513. * Custom user column billing address information. 
  514. * @param array $address Default address. 
  515. * @param int $user_id User id. 
  516. * @return array New address format. 
  517. */ 
  518. public function user_column_billing_address( $address, $user_id ) { 
  519. $address['street_name'] = get_user_meta( $user_id, 'billing_street_name', true ); 
  520. $address['house_number'] = get_user_meta( $user_id, 'billing_house_number', true ); 
  521. $address['house_number_suffix'] = (get_user_meta( $user_id, 'billing_house_number_suffix', true ))?'-'.get_user_meta( $user_id, 'billing_house_number_suffix', true ):''; 
  522.  
  523. return $address; 
  524.  
  525. /** 
  526. * Custom user column shipping address information. 
  527. * @param array $address Default address. 
  528. * @param int $user_id User id. 
  529. * @return array New address format. 
  530. */ 
  531. public function user_column_shipping_address( $address, $user_id ) { 
  532. $address['street_name'] = get_user_meta( $user_id, 'shipping_street_name', true ); 
  533. $address['house_number'] = get_user_meta( $user_id, 'shipping_house_number', true ); 
  534. $address['house_number_suffix'] = (get_user_meta( $user_id, 'shipping_house_number_suffix', true ))?'-'.get_user_meta( $user_id, 'shipping_house_number_suffix', true ):''; 
  535.  
  536. return $address; 
  537.  
  538. /** 
  539. * Custom my address formatted address. 
  540. * @param array $address Default address. 
  541. * @param int $customer_id Customer ID. 
  542. * @param string $name Field name (billing or shipping). 
  543. * @return array New address format. 
  544. */ 
  545. public function my_account_my_address_formatted_address( $address, $customer_id, $name ) { 
  546. $address['street_name'] = get_user_meta( $customer_id, $name . '_street_name', true ); 
  547. $address['house_number'] = get_user_meta( $customer_id, $name . '_house_number', true ); 
  548. $address['house_number_suffix'] = (get_user_meta( $customer_id, $name . '_house_number_suffix', true ))?'-'.get_user_meta( $customer_id, $name . '_house_number_suffix', true ):''; 
  549.  
  550. return $address; 
  551.  
  552. /** 
  553. * Get a posted address field after sanitization and validation. 
  554. * @param string $key 
  555. * @param string $type billing for shipping 
  556. * @return string 
  557. */ 
  558. public function get_posted_address_data( $key, $posted, $type = 'billing' ) { 
  559. if ( 'billing' === $type || false === $posted['ship_to_different_address'] ) { 
  560. $return = isset( $posted[ 'billing_' . $key ] ) ? $posted[ 'billing_' . $key ] : ''; 
  561. } else { 
  562. $return = isset( $posted[ 'shipping_' . $key ] ) ? $posted[ 'shipping_' . $key ] : ''; 
  563.  
  564. return $return; 
  565.  
  566. /** 
  567. * Save order data. 
  568. * @param int $order_id 
  569. * @param array $posted 
  570. * @return void 
  571. */ 
  572. public function save_order_data( $order_id, $posted ) { 
  573. // Billing. 
  574. update_post_meta( $order_id, '_billing_street_name', $this->get_posted_address_data( 'street_name', $posted ) ); 
  575. update_post_meta( $order_id, '_billing_house_number', $this->get_posted_address_data( 'house_number', $posted ) ); 
  576. update_post_meta( $order_id, '_billing_house_number_suffix', $this->get_posted_address_data( 'house_number_suffix', $posted ) ); 
  577.  
  578. // Shipping. 
  579. update_post_meta( $order_id, '_shipping_street_name', $this->get_posted_address_data( 'street_name', $posted, 'shipping' ) ); 
  580. update_post_meta( $order_id, '_shipping_house_number', $this->get_posted_address_data( 'house_number', $posted, 'shipping' ) ); 
  581. update_post_meta( $order_id, '_shipping_house_number_suffix', $this->get_posted_address_data( 'house_number_suffix', $posted, 'shipping' ) ); 
  582.