Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce - How to customize the addresses output?

Tags:

woocommerce

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?

like image 764
joaogdesigner Avatar asked Sep 17 '13 20:09

joaogdesigner


2 Answers

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 );
like image 109
doublesharp Avatar answered Mar 09 '23 01:03

doublesharp


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.

like image 31
Ryszard Jędraszyk Avatar answered Mar 09 '23 01:03

Ryszard Jędraszyk