12
votes

I am customizing the "view order" (order-details.php) page in Woocommerce, within "My Account" (when an user is logged in) and we have the code below to print the billing and shipping addresses:

<?php if (!$order->get_formatted_billing_address()) _e( 'N/A', 'woocommerce' ); else echo $order->get_formatted_billing_address(); ?>

I would like to know if there is a way to customize each item of that output. For example: in the homepage of My Account, which shows the customer's billing and shipping addresses in this way:

<?php
    $address = apply_filters( 'woocommerce_my_account_my_address_formatted_address', array(
        'first_name'    => ucwords(get_user_meta( $customer_id, $name . '_first_name', true )),
        'last_name'     => ucwords(get_user_meta( $customer_id, $name . '_last_name', true )),
        'company'       => ucwords(get_user_meta( $customer_id, $name . '_company', true )),
        'address_1'     => ucwords(get_user_meta( $customer_id, $name . '_address_1', true )),
        'address_2'     => ucwords(get_user_meta( $customer_id, $name . '_address_2', true )),
        'city'          => get_user_meta( $customer_id, $name . '_city', true ),
        'state'         => get_user_meta( $customer_id, $name . '_state', true ),
        'postcode'      => get_user_meta( $customer_id, $name . '_postcode', true ),
        'country'       => get_user_meta( $customer_id, $name . '_country', true )
    ), $customer_id, $name );

    $formatted_address = $woocommerce->countries->get_formatted_address( $address );

    if ( ! $formatted_address )
        _e( 'You have not set up this type of address yet.', 'woocommerce' );
    else
        echo $formatted_address;
?>

It's something like that I want to use in order view page. How could I put that "apply_filters" in this code?

3
This alone works great!Avishai

3 Answers

21
votes

You need to add 3 filters to modify the output of the addresses on the "My Account" page/shortcode for WooCommerce.

First you will need to use woocommerce_my_account_my_address_formatted_address to populate any new values you want to add, for example the user's phone.

add_filter( 'woocommerce_my_account_my_address_formatted_address', function( $args, $customer_id, $name ){
    // the phone is saved as billing_phone and shipping_phone
    $args['phone'] = get_user_meta( $customer_id, $name . '_phone', true );
    return $args;
}, 10, 3 ); 

Next you use woocommerce_localisation_address_formats to modify the formatting of the address - this is determined by the country code - or you can loop through the array to modify all of them, reorganizing or adding fields (the phone).

// modify the address formats
add_filter( 'woocommerce_localisation_address_formats', function( $formats ){
    foreach ( $formats as $key => &$format ) {
        // put a break and then the phone after each format.
        $format .= "\n{phone}";
    }
    return $formats;
} );

Lastly you will need to update woocommerce_formatted_address_replacements to have WooCommerce replace your replacement string with the actual data.

// add the replacement value
add_filter( 'woocommerce_formatted_address_replacements', function( $replacements, $args ){
    // we want to replace {phone} in the format with the data we populated
    $replacements['{phone}'] = $args['phone'];
    return $replacements;
}, 10, 2 );
1
votes

In class-wc-countries.php template default address and addresses for particular countries are specified. Example snippet below placed in your functions.php changes the default address formatting.

function custom_address_formats( $formats ) {
    $formats[ 'default' ]  = "{name}\n{company}\n{address_1} {address_2}\n{postcode} {city}";   
    return $formats;
}
add_filter('woocommerce_localisation_address_formats', 'custom_address_formats');

Have a look into mentioned template to see which address components you would like to include and in what order have them displayed.

0
votes

According to this tutorial, here is an example for Iran country:

add_filter( 'woocommerce_localisation_address_formats', 'woocommerce_custom_address_format', 20 );

function woocommerce_custom_address_format( $formats ) {
    $formats[ 'IR' ]  = "<b>{name}</b>\n";
    $formats[ 'IR' ] .= esc_html__('Province', 'woocommerce') . " {state}";
    $formats[ 'IR' ] .= " ، {city}";
    $formats[ 'IR' ] .= " ، {address_1}\n";
    $formats[ 'IR' ] .= "{company}\n";
    $formats[ 'IR' ] .= esc_html__( 'Postal code:', 'tipikala' ) . " {postcode}";

    return $formats;
}