/includes/wcflespakket-nlpostcode-fields.php

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