【问题标题】:Woocommerce - How to customize the addresses output?Woocommerce - 如何自定义地址输出?
【发布时间】:2013-09-17 20:24:09
【问题描述】:

我正在自定义 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”放在这段代码中?

【问题讨论】:

  • 仅此一项效果很好!

标签: woocommerce


【解决方案1】:

您需要添加 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 );

【讨论】:

  • 这是我在寻找添加自定义字段然后重新排序地址的确切代码。你是我的最爱。泰泰
【解决方案2】:

在 class-wc-countries.php 模板中指定了特定国家的默认地址和地址。 下面的示例 sn-p 放置在您的 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');

查看上述模板,了解您想要包含哪些地址组件以及它们的显示顺序。

【讨论】:

    【解决方案3】:

    根据this tutorial,这里以伊朗国家为例:

    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;
    }
    

    【讨论】:

    • 当我在 'woocommerce_localisation_address_formats' 挂钩中进行 var_dump 时,它会显示所有国家/地区的列表,但是当我像您一样设置该字段时,它将不起作用并显示默认值。其实我想显示完整的州名,但它没有!
    猜你喜欢
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 2022-11-02
    相关资源
    最近更新 更多