gpt4 book ai didi

php - Woocommerce "My Account" "Address Field"显示

转载 作者:太空宇宙 更新时间:2023-11-04 16:23:14 25 4
gpt4 key购买 nike

我正在创建一些自定义的 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 新手 - 所以如果这真的很简单,请耐心等待。我只是在任何地方都没有找到解决方案。通常,我可以轻而易举地解决这类问题。

最佳答案

我知道这有点老了,但我遇到了完全相同的问题,并设法通过使用 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>";

关于php - Woocommerce "My Account" "Address Field"显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27573421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com