Leave a Reply

2 comments

  1. I know this is a little old but I was having the exact same problem and managed to solve it by playing around with the regex that preg_match() searches for.

    All I did was change the $pattern variable and wrap the $output in <p></p> tags. The final code is:

    $address = apply_filters( 'woocommerce_my_account_my_address_formatted_address', array(
            'first_name'  => get_user_meta( $customer_id, $name . '_first_name', true ),
            'last_name'   => get_user_meta( $customer_id, $name . '_last_name', true ),
            'company'     => get_user_meta( $customer_id, $name . '_company', true ),
            'address_1'   => get_user_meta( $customer_id, $name . '_address_1', true ),
            'address_2'   => 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 = WC()->countries->get_formatted_address( $address );
    
        if ( ! $formatted_address )
            _e( 'You have not set up this type of address yet.', 'woocommerce' );
        else
            $pattern = "/<br/>/";
            $replacements= '<p>';
            $formatted_address = WC()->countries->get_formatted_address( $address );
            $output = preg_replace($pattern, $replacements, $formatted_address);
            echo "<p>" . $output . "</p>";
    
  2. If this is what you want

    <address>
            John Doe,John's Company,35 John Street,Johnsville, JD 55555
    </address>
    

    then try this:

    <address>
        <?php
            $address = apply_filters( 'woocommerce_my_account_my_address_formatted_address', array(
                'first_name'  => get_user_meta( $customer_id, $name . '_first_name', true ),
                'last_name'   => get_user_meta( $customer_id, $name . '_last_name', true ),
                'company'     => get_user_meta( $customer_id, $name . '_company', true ),
                'address_1'   => get_user_meta( $customer_id, $name . '_address_1', true ),
                'address_2'   => 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 = WC()->countries->get_formatted_address( $address );
    
            if ( ! $formatted_address )
                _e( 'You have not set up this type of address yet.', 'woocommerce' );
            else
                $pattern = '/<br>/';
                $replacements= ',';
                $formatted_address = WC()->countries->get_formatted_address( $address );
                $output = preg_replace($pattern, $replacements, $formatted_address);
                echo $output;
        ?>
    </address>