如何在 WooCommerce "My Account" 页面上对自定义字段重新排序或添加优先级
How do I re-order or add priority to a custom field on WooCommerce "My Account" page
我在“我的帐户”页面中有多个字段,因此,我想为出生日期添加额外的字段。
我正在使用
的一部分
该字段显示在底部。
我想在我的 2 个现有字段之后添加 DOB 字段,我尝试使用 'priority => 20,' 但它不起作用。
// Add field - my account
function action_woocommerce_edit_account_form() {
woocommerce_form_field( 'birthday_field', array(
'type' => 'date',
'label' => __( 'My Birth Date', 'woocommerce' ),
'placeholder' => __( 'Date of Birth', 'woocommerce' ),
'required' => true,
), get_user_meta( get_current_user_id(), 'birthday_field', true ));
}
add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form' );
// Validate - my account
function action_woocommerce_save_account_details_errors( $args ){
if ( isset($_POST['birthday_field']) && empty($_POST['birthday_field']) ) {
$args->add( 'error', __( 'Please provide a birth date', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
// Save - my account
function action_woocommerce_save_account_details( $user_id ) {
if( isset($_POST['birthday_field']) && ! empty($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
正如在 myaccount/form-edit-account.php 模板文件中所见,woocommerce_edit_account_form
操作挂钩在添加现有字段后执行。
因此,如果您想在现有字段之间添加字段,则必须编辑模板文件
- 可以通过将其复制到
yourtheme/woocommerce/myaccount/form-edit-account.php
来覆盖此模板
您必须删除当前使用的代码
在 myaccount/form-edit-account.php
模板文件中的 2 个现有字段(在您要添加新字段的位置)之间添加此代码
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="account_birthday"><?php esc_html_e( 'Birth day', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="date" class="woocommerce-Input woocommerce-Input--text input-text" name="account_birthday" id="account_birthday" value="<?php echo get_user_meta( get_current_user_id(), 'account_birthday', true ); ?>"/>
</p>
<div class="clear"></div>
- 为了验证和保存然后将以下代码添加到
functions.php
(
或者您喜欢的添加片段的方式)
// Validate - my account
function action_woocommerce_save_account_details_errors( $args ){
if ( isset( $_POST['account_birthday'] ) && empty( $_POST['account_birthday'] ) ) {
$args->add( 'error', __( 'Please provide a birth date', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
// Save - my account
function action_woocommerce_save_account_details( $user_id ) {
if( isset( $_POST['account_birthday'] ) && ! empty( $_POST['account_birthday'] ) ) {
update_user_meta( $user_id, 'account_birthday', sanitize_text_field( $_POST['account_birthday'] ) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
我在“我的帐户”页面中有多个字段,因此,我想为出生日期添加额外的字段。
我正在使用
该字段显示在底部。
我想在我的 2 个现有字段之后添加 DOB 字段,我尝试使用 'priority => 20,' 但它不起作用。
// Add field - my account
function action_woocommerce_edit_account_form() {
woocommerce_form_field( 'birthday_field', array(
'type' => 'date',
'label' => __( 'My Birth Date', 'woocommerce' ),
'placeholder' => __( 'Date of Birth', 'woocommerce' ),
'required' => true,
), get_user_meta( get_current_user_id(), 'birthday_field', true ));
}
add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form' );
// Validate - my account
function action_woocommerce_save_account_details_errors( $args ){
if ( isset($_POST['birthday_field']) && empty($_POST['birthday_field']) ) {
$args->add( 'error', __( 'Please provide a birth date', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
// Save - my account
function action_woocommerce_save_account_details( $user_id ) {
if( isset($_POST['birthday_field']) && ! empty($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
正如在 myaccount/form-edit-account.php 模板文件中所见,woocommerce_edit_account_form
操作挂钩在添加现有字段后执行。
因此,如果您想在现有字段之间添加字段,则必须编辑模板文件
- 可以通过将其复制到
yourtheme/woocommerce/myaccount/form-edit-account.php
来覆盖此模板
您必须删除当前使用的代码
在
myaccount/form-edit-account.php
模板文件中的 2 个现有字段(在您要添加新字段的位置)之间添加此代码
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="account_birthday"><?php esc_html_e( 'Birth day', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="date" class="woocommerce-Input woocommerce-Input--text input-text" name="account_birthday" id="account_birthday" value="<?php echo get_user_meta( get_current_user_id(), 'account_birthday', true ); ?>"/>
</p>
<div class="clear"></div>
- 为了验证和保存然后将以下代码添加到
functions.php
( 或者您喜欢的添加片段的方式)
// Validate - my account
function action_woocommerce_save_account_details_errors( $args ){
if ( isset( $_POST['account_birthday'] ) && empty( $_POST['account_birthday'] ) ) {
$args->add( 'error', __( 'Please provide a birth date', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
// Save - my account
function action_woocommerce_save_account_details( $user_id ) {
if( isset( $_POST['account_birthday'] ) && ! empty( $_POST['account_birthday'] ) ) {
update_user_meta( $user_id, 'account_birthday', sanitize_text_field( $_POST['account_birthday'] ) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );