• Hello,

    I need to sort (alpha) the created addresses. Is there a way ? By adding code in function.php maybe ?

    Thanks for your answer !

Viewing 1 replies (of 1 total)
  • Plugin Author Matt Harrison

    (@matt-h-1)

    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;
    } );
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.