Как добавить новое поле в мои данные учетной записи между другими полями - PullRequest
0 голосов
/ 09 апреля 2020

С помощью приведенного ниже фрагмента мы добавляем поле телефона для выставления счетов к деталям редактирования учетной записи.

Это работает без проблем, только мы хотели бы добавить новое поле между email и поле для смены пароля .

enter image description here

add_action( 'woocommerce_edit_account_form_start', 'misha_add_field_edit_account_form' );
// or add_action( 'woocommerce_edit_account_form', 'misha_add_field_edit_account_form' );
function misha_add_field_edit_account_form() {

    woocommerce_form_field(
        'billing_phone',
        array(
            'type'        => 'text',
            'required'    => true, // remember, this doesn't make the field required, just adds an "*"
            'label'       => 'Phone',
            'description' => 'add your phone number',
        ),
        get_user_meta( get_current_user_id(), 'billing_phone', true ) // get the data
    );

}

/**
 * Step 2. Save field value
 */
add_action( 'woocommerce_save_account_details', 'misha_save_account_details' );
function misha_save_account_details( $user_id ) {

    update_user_meta( $user_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );

}

/**
 * Step 3. Make it required
 */
add_filter('woocommerce_save_account_details_required_fields', 'misha_make_field_required');
function misha_make_field_required( $required_fields ){

    $required_fields['billing_phone'] = 'Phone';
    return $required_fields;

}

1 Ответ

1 голос
/ 09 апреля 2020

Рекомендуется способ применения кода. Однако, поскольку вы хотите добавить новое поле в очень конкретном c месте, я бы рекомендовал следующее

Шаг 1 : перезаписать файл шаблона

https://github.com/woocommerce/woocommerce/blob/3.8.0/templates/myaccount/form-edit-account.php

  • Этот шаблон можно изменить, скопировав его в yourtheme / woocommerce / myaccount / form-edit-account. php .

Заменить: строка 42 - 47

<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
    <label for="account_email"><?php esc_html_e( 'Email address', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
    <input type="email" class="woocommerce-Input woocommerce-Input--email input-text" name="account_email" id="account_email" autocomplete="email" value="<?php echo esc_attr( $user->user_email ); ?>" />
</p>

// ADD NEW FIELD HERE

<fieldset>

С

<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
    <label for="account_email"><?php esc_html_e( 'Email address', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
    <input type="email" class="woocommerce-Input woocommerce-Input--email input-text" name="account_email" id="account_email" autocomplete="email" value="<?php echo esc_attr( $user->user_email ); ?>" />
</p>

<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
    <label for="billing_phone"><?php esc_html_e( 'Billing phone', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
    <input type="tel" class="woocommerce-Input woocommerce-Input--text input-text" name="billing_phone" id="billing_phone" autocomplete="tel" value="<?php echo esc_attr( $user->billing_phone ); ?>" />
</p>

<fieldset>

Шаг 2 и Шаг 3

Чтобы сохранить и подтвердить поле, в котором вы можете сохранить свой существующий код (я их немного скорректировал)

/**
 * Save field value
 */
function my_save_account_details( $user_id ) {
    if( isset( $_POST['billing_phone'] ) ) {
        update_user_meta( $user_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
    }
}
add_action( 'woocommerce_save_account_details', 'my_save_account_details' );

/**
 * Make it required
 */
function my_save_account_details_required_fields( $required_fields ){    
    $required_fields['billing_phone'] = 'Billing phone';
    return $required_fields;
}
add_filter('woocommerce_save_account_details_required_fields', 'my_save_account_details_required_fields');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...