The wc_address_book_addresses
filter allows you to do whatever you like with the array of addresses as it gets them.
Some examples to sort alphabetically. You can adjust them to fit your needs. (Not the prettiest code, but it works)
By Street Address:
add_filter( 'wc_address_book_addresses', function ( $address_book ) {
uasort( $address_book, function ( $address1, $address2 ) {
$address_field1 = null;
foreach ( $address1 as $key => $value ) {
if ( str_contains( $key, 'address_1' ) ) {
$address_field1 = $value;
break;
}
}
$address_field2 = null;
foreach ( $address2 as $key => $value ) {
if ( str_contains( $key, 'address_1' ) ) {
$address_field2 = $value;
break;
}
}
return strcmp( $address_field1, $address_field2 );
});
return $address_book;
} );
By Address Nickname:
add_filter( 'wc_address_book_addresses', function ( $address_book ) {
uasort( $address_book, function ( $address1, $address2 ) {
$address_field1 = null;
foreach ( $address1 as $key => $value ) {
if ( str_contains( $key, 'address_nickname' ) ) {
$address_field1 = $value;
break;
}
}
$address_field2 = null;
foreach ( $address2 as $key => $value ) {
if ( str_contains( $key, 'address_nickname' ) ) {
$address_field2 = $value;
break;
}
}
return strcmp( $address_field1, $address_field2 );
});
return $address_book;
} );