简体   繁体   English

如何在 WooCommerce “我的帐户”页面上对自定义字段重新排序或添加优先级

[英]How do I re-order or add priority to a custom field on WooCommerce “My Account” page

I have multiple fields in "My Account" page, so, I wanted to add additional field for Date of Birth.我在“我的帐户”页面中有多个字段,因此,我想为出生日期添加其他字段。

I am using part of Add birthday field to my account and admin user page我正在使用将生日字段的一部分添加到我的帐户和管理员用户页面

The field shows up but it come in the bottom.该字段显示,但它在底部。

I wanted to add DOB field after my 2 existing fields, I tried using 'priority => 20,' but it doesn't work.我想在我的 2 个现有字段之后添加 DOB 字段,我尝试使用“优先级 => 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 );

As can be seen in myaccount/form-edit-account.php template file the woocommerce_edit_account_form action hook is executed after the already existing fields are added.myaccount/form-edit-account.php模板文件中可以看出,在添加现有字段后执行woocommerce_edit_account_form操作挂钩。

So if you want to add the field between the existing fields you will have to edit the template file因此,如果要在现有字段之间添加字段,则必须编辑模板文件

  • This template can be overridden by copying it to yourtheme/woocommerce/myaccount/form-edit-account.php可以通过将其复制到yourtheme/woocommerce/myaccount/form-edit-account.php来覆盖此模板

  1. You have to delete the code you currently use您必须删除您当前使用的代码

  2. Add this code between 2 existing fields (at the place where you want to add the new field) in the myaccount/form-edit-account.php template filemyaccount/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' ); ?>&nbsp;<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>
  1. For validation and saving then add the following code to functions.php ( or the way you prefer to add snippets)为了验证和保存,然后将以下代码添加到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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM