我正在自定义Woocommerce中的"查看订单"(order-details.php)页面,在"我的帐户"中(当用户登录时),我们有以下代码来打印结算和送货地址:
<?php if (!$order->get_formatted_billing_address()) _e( 'N/A', 'woocommerce' ); else echo $order->get_formatted_billing_address(); ?>
Run Code Online (Sandbox Code Playgroud)
我想知道是否有办法自定义该输出的每个项目.例如:在"我的帐户"的主页中,以这种方式显示客户的帐单和送货地址:
<?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;
?>
Run Code Online (Sandbox Code Playgroud)
这就像我想在订单查看页面中使用的那样.我怎么能在这段代码中加上"apply_filters"?
dou*_*arp 16
您需要添加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 );
Run Code Online (Sandbox Code Playgroud)
接下来,您将使用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;
} );
Run Code Online (Sandbox Code Playgroud)
最后,您需要更新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 );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9700 次 |
| 最近记录: |