forked from crosspeaksoftware/woo-address-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-address-book.php
792 lines (592 loc) · 23.2 KB
/
woocommerce-address-book.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
<?php
/*
* Plugin Name: WooCommerce Address Book
* Description: Gives your customers the option to store multiple shipping addresses and retrieve them on checkout..
* Version: 1.3.1
* Author: Hall Internet Marketing
* Author URI: https://hallme.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Domain Path: /languages
* Text Domain: wc-address-book
*/
// Prevent direct access data leaks.
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$woo_path = 'woocommerce/woocommerce.php';
if ( ! is_plugin_active( $woo_path ) && ! is_plugin_active_for_network( $woo_path ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
/**
* Deactivate the plugin if Google Analytics by Yoast is not active.
*
* @since 1.0.0
*/
function woocommerce_notice__error() {
$class = 'notice notice-error';
$message = __( 'WoooCommerce Address Book requires WooCommerce and has been deactivated.', 'wc-address-book' );
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_attr( $message ) );
}
add_action( 'admin_notices', 'woocommerce_notice__error' );
add_action( 'network_admin_notices', 'woocommerce_notice__error' );
}
// Check if WooCommerce is active.
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
class WC_Address_Book {
/**
* Initializes the plugin by setting localization, filters, and administration functions.
*
* @since 1.0.0
*/
function __construct() {
// Version Number.
$this->version = '1.3.1';
// Load plugin text domain
add_action( 'init', array( $this, 'plugin_textdomain' ) );
// Register hooks that are fired when the plugin is activated, deactivated, and uninstalled, respectively.
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( 'WC_Address_Book', 'deactivate' ) );
register_uninstall_hook( __FILE__, array( 'WC_Address_Book', 'uninstall' ) );
// Enqueue Styles and Scripts
add_action( 'wp_enqueue_scripts', array( $this, 'scripts_styles' ), 20 );
// Save an address to the address book.
add_action( 'woocommerce_customer_save_address', array( $this, 'update_address_names' ), 10, 2 );
add_action( 'woocommerce_customer_save_address', array( $this, 'redirect_on_save' ), 9999, 2 );
// Add custom Shipping Address fields.
add_filter( 'woocommerce_checkout_fields', array( $this, 'shipping_address_select_field' ), 9999, 1 );
// AJAX action to delete an address.
add_action( 'wp_ajax_nopriv_wc_address_book_delete', array( $this, 'wc_address_book_delete' ) );
add_action( 'wp_ajax_wc_address_book_delete', array( $this, 'wc_address_book_delete' ) );
// AJAX action to set a primary address.
add_action( 'wp_ajax_nopriv_wc_address_book_make_primary', array( $this, 'wc_address_book_make_primary' ) );
add_action( 'wp_ajax_wc_address_book_make_primary', array( $this, 'wc_address_book_make_primary' ) );
// AJAX action to refresh the address at checkout.
add_action( 'wp_ajax_nopriv_wc_address_book_checkout_update', array( $this, 'wc_address_book_checkout_update' ) );
add_action( 'wp_ajax_wc_address_book_checkout_update', array( $this, 'wc_address_book_checkout_update' ) );
// Update the customer data with the information entered on checkout.
add_filter ( 'woocommerce_checkout_update_customer_data', array( $this, 'woocommerce_checkout_update_customer_data' ), 10, 2 );
// Add Address Book to Menu
add_filter( 'woocommerce_account_menu_items', array( $this, 'wc_address_book_add_to_menu' ), 10 );
add_action( 'woocommerce_account_edit-address_endpoint', array( $this, 'wc_address_book_page' ), 20 );
// Shipping Address fields
add_filter( 'woocommerce_form_field_country', array( $this, 'shipping_address_country_select' ), 20, 4 );
// Standardize the address edit fields to match Woo's IDs.
add_action( 'woocommerce_form_field_args', array( $this, 'standardize_field_ids' ), 20, 3 );
add_action( 'woocommerce_shipping_fields', array( $this, 'replace_address_key' ), 1001, 2 );
} // end constructor
/**
* Version
*
* @since 1.0.0
*
* @var string
*/
public $version;
/**
* Fired when the plugin is activated.
*
* @param boolean $network_wide - True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog.
* @since 1.0.0
*/
public function activate( $network_wide ) {
// Make sure only admins can wipe the date.
if ( ! current_user_can('activate_plugins') ) {
return;
}
$users = get_users();
foreach ( $users as $user ) {
$address_book = $this->get_address_names( $user->ID );
if ( empty( $address_book ) ) {
$shipping_address = get_user_meta( $user->ID, 'shipping_address_1', true );
if ( ! empty( $shipping_address ) ) {
$this->save_address_names( $user->ID, array( 'shipping' ) );
}
}
}
flush_rewrite_rules();
}
/**
* Fired when the plugin is deactivated.
*
* @param boolean $network_wide - True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog.
* @since 1.0.0
*/
public function deactivate( $network_wide ) {
flush_rewrite_rules();
}
/**
* Fired when the plugin is uninstalled.
*
* @param boolean $network_wide - True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog.
* @since 1.0.0
*/
public function uninstall( $network_wide ) {
flush_rewrite_rules();
}
/**
* Loads the plugin text domain for translation
*
* @since 1.0.0
*/
public function plugin_textdomain() {
load_plugin_textdomain( 'wc-address-book', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
}
/**
* Enqueue scripts and styles
*
* @since 1.0.0
*/
public function scripts_styles() {
if ( ! is_admin() ) {
wp_enqueue_script( 'jquery' );
wp_enqueue_style( 'wc-address-book', plugins_url( '/assets/css/style.css', __FILE__ ), array(), $this->version );
wp_enqueue_script( 'wc-address-book', plugins_url( '/assets/js/scripts.js' , __FILE__ ), array('jquery'), $this->version, true );
wp_localize_script( 'wc-address-book', 'wc_address_book', array(
'ajax_url' => admin_url( 'admin-ajax.php' )
));
}
}
/**
* Adds a link/button to the my account page under the addresses for adding additional addresses to their account.
*
* @since 1.0.0
*/
public function add_additional_address_button() {
$user_id = get_current_user_id();
$address_names = $this->get_address_names( $user_id );
$name = $this->set_new_address_name( $address_names );
?>
<div class="add-new-address">
<a href="<?php echo wc_get_endpoint_url( 'edit-address', 'shipping/?address-book=' . $name ); ?>" class="add button"><?php _e( 'Add New Shipping Address', 'wc-address-book' ); ?></a>
</div>
<?php
}
/**
* Returns the next available shipping address name.
*
* @param string $address_names - An array of saved address names.
* @since 1.0.0
*/
public function set_new_address_name( $address_names ) {
// Check the address book entries and add a new one.
if ( isset( $address_names ) && ! empty( $address_names ) ) {
$new = str_replace('shipping', '', end( $address_names ));
$name = 'shipping' . intval ( $new + 1, 10 );
} else { // Start the address book.
$name = 'shipping2';
}
return $name;
}
/**
* Replace My Address with the Address Book to My Account Menu.
*
* @param Array $items - An array of menu items.
* @since 1.0.0
*/
function wc_address_book_add_to_menu( $items ) {
$new_items = array();
foreach ($items as $key => $value) {
if ( $key === "edit-address" ) {
$new_items[$key] = "Address Book";
} else {
$new_items[$key] = $value;
}
}
return $new_items;
}
/**
* Adds Address Book Content.
*
* @since 1.0.0
*/
public function wc_address_book_page( $type ) {
wc_get_template( 'myaccount/my-address-book.php', array('type' => $type ), '', plugin_dir_path( __FILE__ ) . 'templates/' );
}
/**
* Modify the shipping address field to allow for available countries to displayed correctly. Overides most of woocommerce_form_field().
*
* @since 1.0.0
*/
public function shipping_address_country_select( $field, $key, $args, $value ) {
if ( $args['required'] && ! in_array( 'validate-required', $args['class'] ) ) {
$args['class'][] = 'validate-required';
$required = ' <abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>';
} else {
$required = '';
}
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
$args['autocomplete'] = ( $args['autocomplete'] ) ? 'autocomplete="' . esc_attr( $args['autocomplete'] ) . '"' : '';
if ( is_string( $args['label_class'] ) ) {
$args['label_class'] = array( $args['label_class'] );
}
if ( is_null( $value ) ) {
$value = $args['default'];
}
// Custom attribute handling
$custom_attributes = array();
if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) {
foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
}
}
if ( ! empty( $args['validate'] ) ) {
foreach( $args['validate'] as $validate ) {
$args['class'][] = 'validate-' . $validate;
}
}
$field = '';
$label_id = $args['id'];
$sort = $args['priority'] ? $args['priority'] : '';
$field_container = '<p class="form-row %1$s" id="%2$s" data-priority="' . esc_attr( $sort ) . '">%3$s</p>';
/**
* HALL EDIT: The primary purpose for this override is to replace the default 'shipping_country' with 'billing_country'.
*/
$countries = 'billing_country' === $key ? WC()->countries->get_allowed_countries() : WC()->countries->get_shipping_countries();
if ( 1 === count( $countries ) ) {
$field .= '<strong>' . current( array_values( $countries ) ) . '</strong>';
$field .= '<input type="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="' . current( array_keys( $countries ) ) . '" ' . implode( ' ', $custom_attributes ) . ' class="country_to_state" readonly="readonly" />';
} else {
$field = '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="country_to_state country_select ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . '><option value="">' . esc_html__( 'Select a country…', 'woocommerce' ) . '</option>';
foreach ( $countries as $ckey => $cvalue ) {
$field .= '<option value="' . esc_attr( $ckey ) . '" ' . selected( $value, $ckey, false ) . '>' . $cvalue . '</option>';
}
$field .= '</select>';
$field .= '<noscript><input type="submit" name="woocommerce_checkout_update_totals" value="' . esc_attr__( 'Update country', 'woocommerce' ) . '" /></noscript>';
}
if ( ! empty( $field ) ) {
$field_html = '';
if ( $args['label'] && 'checkbox' != $args['type'] ) {
$field_html .= '<label for="' . esc_attr( $label_id ) . '" class="' . esc_attr( implode( ' ', $args['label_class'] ) ) . '">' . $args['label'] . $required . '</label>';
}
$field_html .= $field;
if ( $args['description'] ) {
$field_html .= '<span class="description">' . esc_html( $args['description'] ) . '</span>';
}
$container_class = esc_attr( implode( ' ', $args['class'] ) );
$container_id = esc_attr( $args['id'] ) . '_field';
$field = sprintf( $field_container, $container_class, $container_id, $field_html );
}
if ( $args['return'] ) {
return $field;
} else {
echo $field;
}
}
/**
* Update Address Book Values
*
* @param int $user_id - User's ID
* @param string $name - The name of the address being updated.
* @since 1.0.0
*/
public function update_address_names( $user_id, $name ) {
if ( isset( $_GET['address-book'] ) ) {
$name = $_GET['address-book'];
}
// Only save shipping addresses.
if ( "billing" === $name ) {
return;
}
// Get the address book and update the label.
$address_names = $this->get_address_names( $user_id );
// Build new array if one does not exist.
if ( ! is_array( $address_names ) || empty( $address_names ) ) {
$address_names = array();
}
// Add shipping name if not already in array.
if ( ! in_array( $name, $address_names ) ) {
array_push( $address_names, $name );
$this->save_address_names( $user_id, $address_names );
}
}
/**
* Redirect to the Edit Address page on save. Overrides the default redirect to /my-account/
*
* @param int $user_id - User's ID
* @param string $name - The name of the address being updated.
* @since 1.0.0
*/
public function redirect_on_save( $user_id, $name ) {
if ( ! is_admin() && ! defined( 'DOING_AJAX' ) ) {
wp_safe_redirect( wc_get_account_endpoint_url( 'edit-address' ) );
exit;
}
}
/**
* Returns an array of the customer's address names.
*
* @param int $user_id - User's ID
* @since 1.0.0
*/
public function get_address_names( $user_id = null ) {
$address_names = get_user_meta( $user_id, 'wc_address_book', true);
return $address_names;
}
/**
* Returns an array of the customer's addresses with field values.
*
* @param int $user_id - User's ID
* @since 1.0.0
*/
public function get_address_book( $user_id = null ){
$countries = new WC_Countries();
if ( ! isset( $country ) ) {
$country = $countries->get_base_country();
}
if ( ! isset( $user_id ) ) {
$user = wp_get_current_user();
$user_id = $user->ID;
}
$address_names = $this->get_address_names( $user_id );
$address_fields = WC()->countries->get_address_fields( $country, 'shipping_' );
// Get the set shipping fields, including any custom values.
$address_keys = array_keys( $address_fields );
$address_book = array();
if ( ! empty( $address_names ) ) {
foreach ( $address_names as $name ) {
// Do not include the billing address
if ( $name == 'billing' ) {
continue;
}
unset($address);
foreach ( $address_keys as $field ) {
// Remove the default name so the custom ones can be added.
$field = str_replace( 'shipping', '', $field );
$address[$name . $field] = get_user_meta( $user_id, $name . $field, true );
}
$address_book[$name] = $address;
}
}
return $address_book;
}
/**
* Returns an array of the users/customer additional address key value pairs.
*
* @param int $user_id - User's ID
* @since 1.0.0
*/
public function save_address_names( $user_id, $new_value ){
// Make sure that is a new_value to save.
if ( ! isset( $new_value ) ) {
return;
}
// Update the value.
$error_test = update_user_meta( $user_id, 'wc_address_book', $new_value );
// If update_user_meta returns false, throw an error.
if( !$error_test ) {
// TODO: Add error notice.
}
}
/**
* Adds the address book select to the checkout page.
*
* @param array $fields - An array of WooCommerce Shipping Address fields.
* @since 1.0.0
*/
public function shipping_address_select_field( $fields ) {
$address_book = $this->get_address_book();
$customer_id = wp_get_current_user();
$address_selector['address_book'] = array(
'type' => 'select',
'class' => array( 'form-row-wide', 'address_book' ),
'label' => __( 'Address Book', 'woocommerce' ),
'order' => -1,
'priority' => -1
);
if ( ! empty( $address_book ) && false !== $address_book ) {
foreach ( $address_book as $name => $address) {
if ( ! empty( $address[$name . '_address_1'] ) ) {
$address_selector['address_book']['options'][$name] = $this->address_select_label( $address, $name );
}
}
}
$address_selector['address_book']['options']['add_new'] = 'Add New Address';
$fields['shipping'] = $address_selector + $fields['shipping'];
return $fields;
}
/**
* Adds the address book select to the checkout page.
*
* @param array $address - An array of WooCommerce Shipping Address data.
* @since 1.0.0
*/
function address_select_label( $address, $name ) {
$show_state = ( $address[$name . '_state'] ? true : false );
$label = $address[$name . '_first_name'] . ' ' . $address[$name . '_last_name'];
$label .= ( $address[$name . '_address_1'] ? ', ' . $address[$name . '_address_1'] : '' );
$label .= ( $address[$name . '_city'] ? ', ' . $address[$name . '_city'] : '' );
$label .= ( $address[$name . '_state'] ? ', ' . $address[$name . '_state'] : '' );
return apply_filters( 'wc_address_book_address_select_label', $label );
}
/**
* Used for deleting addresses from the my-account page.
*
* @param string $address_name - The name of a specific address in the address book.
* @since 1.0.0
*/
function wc_address_book_delete( $address_name ) {
$address_name = $_POST['name'];
$customer_id = get_current_user_id();
$address_book = $this->get_address_book( $customer_id );
$address_names = $this->get_address_names( $customer_id );
foreach ( $address_book as $name => $address ) {
if ( $address_name === $name ) {
// Remove address from address book.
if ( ( $key = array_search( $name, $address_names ) ) !== false ) {
unset( $address_names[$key] );
}
$this->save_address_names( $customer_id, $address_names );
// Remove specific address values.
foreach ( $address as $field => $value ) {
delete_user_meta( $customer_id, $field );
}
break;
}
}
if ( is_ajax() ) {
die();
}
}
/**
* Used for setting the primary shipping addresses from the my-account page.
*
* @since 1.0.0
*/
function wc_address_book_make_primary() {
$customer_id = get_current_user_id();
$address_book = $this->get_address_book( $customer_id );
$primary_address_name = 'shipping';
$alt_address_name = $_POST['name'];
// Loop through and swap values between shipping names.
foreach ( $address_book[$primary_address_name] as $field => $value ) {
$alt_field = preg_replace('/^[^_]*_\s*/', $alt_address_name . '_', $field);
$resp = update_user_meta( $customer_id, $field, $address_book[$alt_address_name][$alt_field] );
}
foreach ( $address_book[$alt_address_name] as $field => $value ) {
$primary_field = preg_replace('/^[^_]*_\s*/', $primary_address_name . '_', $field);
$resp = update_user_meta( $customer_id, $field, $address_book[$primary_address_name][$primary_field] );
}
die();
}
/**
* Used for updating addresses dynamically on the checkout page.
*
* @since 1.0.0
*/
function wc_address_book_checkout_update() {
global $woocommerce;
$name = $_POST['name'];
$address_book = $this->get_address_book();
$customer_id = get_current_user_id();
$shipping_countries = $woocommerce->countries->get_shipping_countries();
$response = array();
// Get address field values.
if ( 'add_new' !== $name ) {
foreach ( $address_book[$name] as $field => $value ) {
$field = preg_replace('/^[^_]*_\s*/', 'shipping_', $field);
$response[$field] = $value;
}
} else {
// If only one country is available for shipping, include it in the blank form.
if ( 1 === count( $shipping_countries ) ) {
$response['shipping_country'] = key( $shipping_countries );
}
}
echo json_encode( $response );
die();
}
/**
* Update the customer data with the information entered on checkout.
*
* @param boolean $update_customer_data - Toggles whether Woo should update customer data on checkout. This plugin overrides that function entirely.
*
* @param Object $checkout_object - An object of the checkout fields and values.
*
* @since 1.0.0
*/
public function woocommerce_checkout_update_customer_data( $update_customer_data, $checkout_object ) {
$name = $checkout_object->posted['address_book'];
$user = wp_get_current_user();
$address_book = $this->get_address_book( $user->ID );
$update_customer_data = false;
$ignore_shipping_address = false;
if ( $checkout_object->posted['ship_to_different_address'] != true ) {
$ignore_shipping_address = true;
}
// Name new address and update address book.
if ( ( 'add_new' === $name || ! isset( $name ) ) && false === $ignore_shipping_address ) {
$address_names = $this->get_address_names( $user->ID );
$name = $this->set_new_address_name( $address_names );
}
if ( false === $ignore_shipping_address ) {
$this->update_address_names( $user->ID, $name );
}
// Billing address
$billing_address = array();
if ( $checkout_object->checkout_fields['billing'] ) {
foreach ( array_keys( $checkout_object->checkout_fields['billing'] ) as $field ) {
$field_name = str_replace( 'billing_', '', $field );
$billing_address[ $field_name ] = $checkout_object->get_posted_address_data( $field_name );
}
}
// Shipping address.
$shipping_address = array();
if ( $checkout_object->checkout_fields['shipping'] ) {
foreach ( array_keys( $checkout_object->checkout_fields['shipping'] ) as $field ) {
$field_name = str_replace( 'shipping_', '', $field );
// Prevent address book and label fields from being written to the DB.
if ( 'address_book' === $field_name || 'address_label' === $field_name ) {
continue;
}
$shipping_address[ $field_name ] = $checkout_object->get_posted_address_data( $field_name, 'shipping' );
}
}
foreach ( $billing_address as $key => $value ) {
update_user_meta( $user->ID, 'billing_' . $key, $value );
}
if ( WC()->cart->needs_shipping() && false === $ignore_shipping_address ) {
foreach ( $shipping_address as $key => $value ) {
update_user_meta( $user->ID, $name . '_' . $key, $value );
}
}
return $update_customer_data;
}
/**
* Standardize the address edit fields to match Woo's IDs.
*
* @param Array $args - The set of arguments being passed to the field.
*
* @param String $key - The name of the address being edited.
*
* @param String $value - The value a field will be prepopulated with.
*
* @since 1.0.0
*/
function standardize_field_ids( $args, $key, $value ) {
if ( 'address_book' != $key ) {
$args['id'] = preg_replace( '/^shipping[^_]/', 'shipping', $args['id'] );
}
return $args;
}
/**
* Replace the standard 'Shipping' address key with address book key.
*
* @param Array $address_fields - The set of WooCommerce Address Fields.
*
* @since 1.1.0
*/
function replace_address_key( $address_fields ) {
if ( isset( $_GET['address-book'] ) ) {
foreach ( $address_fields as $key => $value ) {
$newkey = str_replace( 'shipping', esc_attr( $_GET['address-book'] ), $key );
$address_fields[$newkey] = $address_fields[$key];
unset( $address_fields[$key] );
}
}
return $address_fields;
}
} // end class
// Init Class
$wc_address_book = new WC_Address_Book();
}