wordpress woocommerce - 在结帐页面上显示和修改帐户字段

wordpress woocommerce - show and modify account fields on checkout page

我知道您可以在 WooCommerce 的结帐页面上添加自定义字段,但我想在账单明细之前显示的是帐户字段,这些字段已经存在,如 documentation 中所写。这些字段被命名为:

但它们默认不显示。我只是设法让它们可见,方法是将它们放在函数的列表顶部,以便在我的主题 function.php 中像这样

重新排序帐单字段
add_filter("woocommerce_checkout_fields", "order_fields");

function order_fields($fields) {

    $order = array(
        "account_username",
        "account_password",
        "account_password-2",
        "billing_first_name",
        "billing_last_name",
        // other billing fields go here
    );

    foreach($order as $field)
    {
        $ordered_fields[$field] = $fields["billing"][$field];
    }

    $fields["billing"] = $ordered_fields;
    return $fields;

}

这与结账时创建帐户的功能配合得很好,但我在修改其标签和占位符时遇到了麻烦。这就是我试图做的:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {

    $fields['account']['account_username']['label'] = '* Username: ';
    $fields['account']['account_username']['placeholder'] = 'Enter username here...';

}

但它不允许我更改字段的标签和占位符,所以我想这可能与我如何显示它有关and/or我如何修改它们。

有想法吗?提前致谢。

我已经找到了这个问题的答案,所以如果有人遇到同样的问题,这是最好的解决方案。在我的例子中,与其尝试使帐户字段可见,不如手动输出我需要的字段更有效,因为我无论如何都不需要大多数默认字段。

我所做的是覆盖 form-billing.php 模板。我删除了这部分字段的循环:

<?php foreach ( $checkout->checkout_fields['billing'] as $key => $field ) : ?>

    <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>

<?php endforeach; ?>

并将其替换为将它们单独添加到页面中:

<?php
    woocommerce_form_field( 'billing_first_name', $checkout->checkout_fields['billing']['billing_first_name'], $checkout->get_value( 'billing_first_name') );
    woocommerce_form_field( 'billing_email', $checkout->checkout_fields['billing']['billing_email'], $checkout->get_value( 'billing_email') );
    woocommerce_form_field( 'account_username', $checkout->checkout_fields['account']['account_username'], $checkout->get_value( 'account_username') );
    woocommerce_form_field( 'account_password', $checkout->checkout_fields['account']['account_password'], $checkout->get_value( 'account_password') );
    woocommerce_form_field( 'account_password-2', $checkout->checkout_fields['account']['account_password-2'], $checkout->get_value( 'account_password-2') );
    //...other fields that I need
?>

从那里开始,对标签、占位符等的修改工作正常。希望它也适用于有同样问题的其他人。干杯! :)