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

时间:2014-12-19 20:27:47

标签: php html arrays wordpress account

我正在创建一些自定义的Woocommerce我的帐户页面。一切都很好,我遇到了一些我无法解决的麻烦的小代码。

我的目标是简单地在$address = apply_filters( 'woocommerce_my_account_my_address_formatted_address', ...

的输出之间添加间距

目前,它会在字段值之间填充<br>(甚至不是<br />)个标记。我真的希望能够用<br>标记替换<p>标记。而已。如果我能理解如何在它们之间添加我想要的东西会很好,但这并不重要。

所以,这是件:

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

然后输出以下内容:

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

有人陪我吗?我确实是一个PHP新手 - 所以如果这很简单,那么请耐心等待。我还没有找到解决方案。通常,我可以通过这种方式摆脱困境。

2 个答案:

答案 0 :(得分:3)

我知道这有点老了,但我遇到了完全相同的问题并设法通过使用preg_match()搜索的正则表达来解决它。

我所做的就是更改$pattern变量并将$output包裹在<p></p>标记中。最终的代码是:

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

答案 1 :(得分:0)

如果这是你想要的

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

然后试试这个:

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