gpt4 book ai didi

Woocommerce - 如何自定义地址输出?

转载 作者:行者123 更新时间:2023-12-03 17:43:48 24 4
gpt4 key购买 nike

我正在 Woocommerce 中的“我的帐户”(当用户登录时)中自定义“查看订单”(order-details.php)页面,我们有下面的代码来打印帐单和送货地址:

<?php if (!$order->get_formatted_billing_address()) _e( 'N/A', 'woocommerce' ); else echo $order->get_formatted_billing_address(); ?>

我想知道是否有办法自定义该输出的每个项目。例如:在我的账户首页,以这种方式显示客户的账单地址和送货地址:
<?php
$address = apply_filters( 'woocommerce_my_account_my_address_formatted_address', array(
'first_name' => ucwords(get_user_meta( $customer_id, $name . '_first_name', true )),
'last_name' => ucwords(get_user_meta( $customer_id, $name . '_last_name', true )),
'company' => ucwords(get_user_meta( $customer_id, $name . '_company', true )),
'address_1' => ucwords(get_user_meta( $customer_id, $name . '_address_1', true )),
'address_2' => ucwords(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 = $woocommerce->countries->get_formatted_address( $address );

if ( ! $formatted_address )
_e( 'You have not set up this type of address yet.', 'woocommerce' );
else
echo $formatted_address;
?>

这就像我想在订单查看页面中使用的东西。我怎么能把“apply_filters”放在这段代码中?

最佳答案

您需要添加 3 个过滤器来修改 WooCommerce 的“我的帐户”页面/短代码上地址的输出。

首先,您需要使用 woocommerce_my_account_my_address_formatted_address填充您要添加的任何新值,例如用户的电话。

add_filter( 'woocommerce_my_account_my_address_formatted_address', function( $args, $customer_id, $name ){
// the phone is saved as billing_phone and shipping_phone
$args['phone'] = get_user_meta( $customer_id, $name . '_phone', true );
return $args;
}, 10, 3 );

接下来你用 woocommerce_localisation_address_formats修改地址的格式 - 这由国家/地区代码决定 - 或者您可以遍历数组以修改所有地址,重新组织或添加字段(电话)。
// modify the address formats
add_filter( 'woocommerce_localisation_address_formats', function( $formats ){
foreach ( $formats as $key => &$format ) {
// put a break and then the phone after each format.
$format .= "\n{phone}";
}
return $formats;
} );

最后你需要更新 woocommerce_formatted_address_replacements让 WooCommerce 用实际数据替换您的替换字符串。
// add the replacement value
add_filter( 'woocommerce_formatted_address_replacements', function( $replacements, $args ){
// we want to replace {phone} in the format with the data we populated
$replacements['{phone}'] = $args['phone'];
return $replacements;
}, 10, 2 );

关于Woocommerce - 如何自定义地址输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18859237/

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