通过 WooCommerce 中的 my-account/my-address.php 模板文件更改地址的显示方式
Change the way the address is displayed via my-account/my-address.php template file in WooCommerce
我想修改 $address
的输出,可以在 /myaccount/my-address.php 模板文件的第 67 行 (@version 2.6.0) 找到。
模板文件本身有以下代码:
<address>
<?php
echo $address ? wp_kses_post( $address ) : esc_html_e( 'You have not set up this type of address yet.', 'woocommerce' );
?>
</address>
这段代码的输出例如是:
Marke
1 Frazer Lane, New Oxford,pa, 13350 United States
但我正在尝试为每一行添加一个标题,例如:
Name: Marke
Address: 1 Frazer Lane, New Oxford,pa, 13350 United States
所以我在模板文件中应用了以下调整:
<address>
<?php
if ( $address) {
// Split a string by a string
$myAddr= explode( '<br/>', $address);
// Loop
foreach ( $pieces as $piece ) {
echo '<p>' . 'Title: ' . $myAddr. '</p>';
}
} else {
echo esc_html_e( 'You have not set up this type of address yet.', 'woocommerce' );
}
?>
</address>
在这里我可以只添加<br>
标签来使信息清晰,但我不能为每一行添加不同的标题。有什么建议吗?
您确实可以覆盖模板文件。然而,另一种选择是使用 woocommerce_my_account_my_address_formatted_address
过滤器挂钩。
在 key
的基础上,您可以添加前缀,以及删除字段(通过 unset()
)或合并。简而言之:这取决于您的具体愿望。
所以你得到:
function filter_woocommerce_my_account_my_address_formatted_address( $address, $customer_id, $address_type ) {
// Loop
foreach ( $address as $key => $part ) {
// First name
if ( $key == 'first_name' ) {
// Add prefix
$address[$key] = __( 'Name: ', 'woocommerce' ) . $part;
// Address 1
} elseif ( $key == 'address_1' ) {
// Add prefix
$address[$key] = __( 'Address: ', 'woocommerce' ) . $part;
}
}
return $address;
}
add_filter( 'woocommerce_my_account_my_address_formatted_address', 'filter_woocommerce_my_account_my_address_formatted_address', 10, 3 );
代码进入活动子主题(或活动主题)的 functions.php 文件。
结果:
我想修改 $address
的输出,可以在 /myaccount/my-address.php 模板文件的第 67 行 (@version 2.6.0) 找到。
模板文件本身有以下代码:
<address>
<?php
echo $address ? wp_kses_post( $address ) : esc_html_e( 'You have not set up this type of address yet.', 'woocommerce' );
?>
</address>
这段代码的输出例如是:
Marke
1 Frazer Lane, New Oxford,pa, 13350 United States
但我正在尝试为每一行添加一个标题,例如:
Name: Marke
Address: 1 Frazer Lane, New Oxford,pa, 13350 United States
所以我在模板文件中应用了以下调整:
<address>
<?php
if ( $address) {
// Split a string by a string
$myAddr= explode( '<br/>', $address);
// Loop
foreach ( $pieces as $piece ) {
echo '<p>' . 'Title: ' . $myAddr. '</p>';
}
} else {
echo esc_html_e( 'You have not set up this type of address yet.', 'woocommerce' );
}
?>
</address>
在这里我可以只添加<br>
标签来使信息清晰,但我不能为每一行添加不同的标题。有什么建议吗?
您确实可以覆盖模板文件。然而,另一种选择是使用 woocommerce_my_account_my_address_formatted_address
过滤器挂钩。
在 key
的基础上,您可以添加前缀,以及删除字段(通过 unset()
)或合并。简而言之:这取决于您的具体愿望。
所以你得到:
function filter_woocommerce_my_account_my_address_formatted_address( $address, $customer_id, $address_type ) {
// Loop
foreach ( $address as $key => $part ) {
// First name
if ( $key == 'first_name' ) {
// Add prefix
$address[$key] = __( 'Name: ', 'woocommerce' ) . $part;
// Address 1
} elseif ( $key == 'address_1' ) {
// Add prefix
$address[$key] = __( 'Address: ', 'woocommerce' ) . $part;
}
}
return $address;
}
add_filter( 'woocommerce_my_account_my_address_formatted_address', 'filter_woocommerce_my_account_my_address_formatted_address', 10, 3 );
代码进入活动子主题(或活动主题)的 functions.php 文件。
结果: