wordpress woocommerce - 在结帐页面上显示和修改帐户字段 [英] wordpress woocommerce - show and modify account fields on checkout page

查看:36
本文介绍了wordpress woocommerce - 在结帐页面上显示和修改帐户字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I understand that you can add custom fields on the checkout page of WooCommerce, but what I want to show before the billing details are the account fields, which are already existing as written in the documentation. These fields are named:

  • account_username
  • account_password
  • account_password-2

但默认情况下它们不会显示.我只是通过将它们放在函数列表的顶部来使它们可见,以便在我的主题的 function.php 像这样重新排序计费字段

But they are not shown by default. I only managed to make them visible by putting them at the top of the list in the function for reordering the billing fields like this in my theme's function.php like this

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;

}

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

This works fine with the functionality of creating an account while checking out, but I am having troubles with modifying its label and placeholder. This is what I tried to do:

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...';

}

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

But it wouldn't let me change the labels and placeholder of the fields so I was thinking that maybe it has something to do with how I am displaying it and/or how I am modifying them.

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

Ideas, anyone? Thanks in advance.

推荐答案

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

I've found the answer to this, so if anyone has the same problem, this is the best solution. Instead of trying to make the accounts fields visible, in my case it's more efficient to manually output the fields I need since I won't be needing most of the default fields anyway.

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

What I did was override the form-billing.php template. I removed the loop for the fields which is this part:

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

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

<?php endforeach; ?>

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

And replaced it with this to individually add them to the page:

<?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
?>

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

From there, the modifications for the label, placeholder, etc. works just fine. Hope it works for other people with the same issues too. Cheers! :)

这篇关于wordpress woocommerce - 在结帐页面上显示和修改帐户字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆