Woocommerce“我的帐户”“地址字段”显示

I'm creating some custom Woocommerce My Account Pages. Everything's going great, and I run into a troublesome little chunk of code that I can't get through.

My goal is to simply add spacing between the output of the $address = apply_filters( 'woocommerce_my_account_my_address_formatted_address', ...

Currently, it populates <br> (not even <br />) tags between the field values. I really want to be able to replace the <br> tags with <p> tags. That's it. It would be nice if I could understand how to add whatever I wanted between them, but that's not as important.

So, here are the pieces:

<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
                echo $formatted_address;
        ?>
    </address>

This then outputs the following:

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

Anyone with me? I am a PHP novice, for sure - so if this is really simple, then bear with me. I just haven't found a solution anywhere regarding this. Usually, I can weasle my way through this kind of stuff.

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>

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>";