首页 文章

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

提问于
浏览
0

我了解您可以在WooCommerce的结帐页面上添加自定义字段,但我想在结算明细之前显示的是帐户字段,这些字段已经存在于documentation中 . 这些字段命名为:

  • account_username

  • account_password

  • account_password-2

但默认情况下不会显示它们 . 我只是通过将它们放在函数列表的顶部,以便在我的主题 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...';

}

但是它不会让我更改字段的标签和占位符,所以我在想它可能与我如何显示它和/或我如何修改它们有关 .

想法,有人吗?提前致谢 .

1 回答

  • 2

    我找到了答案,所以如果有人遇到同样的问题,这是最好的解决方案 . 而不是尝试使帐户字段可见,在我的情况下,手动输出我需要的字段更有效,因为我不需要大多数默认字段 .

    我做的是覆盖 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
    ?>
    

    从那里,对标签,占位符等的修改工作得很好 . 希望它也适用于同样问题的其他人 . 干杯! :)

相关问题