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

Woocommerce-如何自定义地址输出?,woocommerce,Woocommerce,我正在自定义Woocommerce中“我的帐户”(当用户登录时)中的“查看订单”(order details.php)页面,我们有下面的代码来打印账单和发货地址: <?php if (!$order->get_formatted_billing_address()) _e( 'N/A', 'woocommerce' ); else echo $order->get_formatted_billing_address(); ?> 我想知道是否有一种方法可以定制该输出的

我正在自定义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;
?>


这是我想在订单查看页面中使用的东西。如何在代码中添加“应用过滤器”?

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

首先,您需要使用
woocommerce\u my\u account\u my\u address\u formatted\u 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\u localization\u address\u formats
修改地址的格式-这由国家/地区代码决定-或者您可以通过数组循环修改所有地址,重新组织或添加字段(手机)

最后,您需要更新
woocommerce\u格式化的\u地址\u替换内容
,让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 );

在class-wc-countries.php模板中,指定了默认地址和特定国家/地区的地址。 下面放在functions.php中的示例代码段更改了默认地址格式

function custom_address_formats( $formats ) {
    $formats[ 'default' ]  = "{name}\n{company}\n{address_1} {address_2}\n{postcode} {city}";   
    return $formats;
}
add_filter('woocommerce_localisation_address_formats', 'custom_address_formats');
查看上述模板,查看您希望包括哪些地址组件以及它们的显示顺序。

根据,以下是伊朗国家的示例:

add_filter( 'woocommerce_localisation_address_formats', 'woocommerce_custom_address_format', 20 );

function woocommerce_custom_address_format( $formats ) {
    $formats[ 'IR' ]  = "<b>{name}</b>\n";
    $formats[ 'IR' ] .= esc_html__('Province', 'woocommerce') . " {state}";
    $formats[ 'IR' ] .= " ، {city}";
    $formats[ 'IR' ] .= " ، {address_1}\n";
    $formats[ 'IR' ] .= "{company}\n";
    $formats[ 'IR' ] .= esc_html__( 'Postal code:', 'tipikala' ) . " {postcode}";

    return $formats;
}
add_filter('woocommerce_localization_address_format'、'woocommerce_custom_address_format',20);
功能商业\自定义\地址\格式($formats){
$formats['IR']=“{name}\n”;
$formats['IR'].=esc_html_uuuuu('Province','woocommerce')。“{state}”;
$formats['IR']。=“{city}”;
$formats['IR'].=“{address_1}\n”;
$formats['IR'].=“{company}\n”;
$formats['IR'].=esc_html_uuu('Postal code:','tipikala')。“{postacode}”;
返回$格式;
}

这正是我想要添加自定义字段然后重新排序地址的代码。你是我的最爱。泰蒂:单凭这一点就很好!当我在“woocommerce\u Localization\u address\u formats”钩子中输入var\u dump时,它会显示所有国家/地区的列表,但当我像您那样设置字段时,它将不起作用并显示默认的国家/地区。事实上,我想显示完整的州名,但它没有!
add_filter( 'woocommerce_localisation_address_formats', 'woocommerce_custom_address_format', 20 );

function woocommerce_custom_address_format( $formats ) {
    $formats[ 'IR' ]  = "<b>{name}</b>\n";
    $formats[ 'IR' ] .= esc_html__('Province', 'woocommerce') . " {state}";
    $formats[ 'IR' ] .= " ، {city}";
    $formats[ 'IR' ] .= " ، {address_1}\n";
    $formats[ 'IR' ] .= "{company}\n";
    $formats[ 'IR' ] .= esc_html__( 'Postal code:', 'tipikala' ) . " {postcode}";

    return $formats;
}