-1

I have customized the myaccount/form-edit-account.php WooCommerce template file. So that a customer in addition to the default fields first name, last name, display name, email and password. Now also has the option to add/edit his nickname.

Once logged in, he can access and edit that information on his account page.

Here the code I have added in the myaccount/form-edit-account.php on line 35 (between the last name and the display name field.

<p class="woocommerce-form-row woocommerce-form-row--last form-row form-row-wide">
    <label for="account_nickname"><?php esc_html_e( 'Téléphone (non modifiable. Si erreur contacer Laura au 07.66.89.85.05) ', 'woocommerce' ); ?>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_nickname" id="account_nickname" autocomplete="family-name" value="<?php echo esc_attr( $user->nickname ); ?>" />
</p>
<div class="clear"></div>

Unfortunatly, when he wants to modify the nickname, and clicks on the submit button, no error occurs, but the nickname is not updated.

Someone who can tell me what further adjustments are needed? am I doing something wrong somewhere?

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • 1
    All you have done so far (at least in what you have shown), is add an input field to the form on the client side. Did you really expect, that would automatically cause something on the server side to get updated? If that was the case, then I could add any arbitrary field to the form via my browser dev tools, submit … and your system would update whatever database field might correspond to the field name. That that’s not actually supposed to happen, should be pretty obvious. – misorude Jan 15 '21 at 12:36
  • Your question is so far away from a solution that could be posted here that I recommend to study the WooCommerce https://docs.woocommerce.com/documentation/plugins/woocommerce/ docs throughly before "getting your hands on implementing something". There is much more that you will have to consider, extending the form is just one last bit to be done. – olidem Jan 16 '21 at 10:46

1 Answers1

0

First of all, instead of using your custom code, use following code to add the field to the myaccount/form-edit-account.php template file (On line 35, or elsewhere if desired, but in any case between existing fields.

  • Note: This template can be overridden by copying it to yourtheme/woocommerce/myaccount/form-edit-account.php.
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
    <label for="account_nick_name"><?php esc_html_e( 'Nickname', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_nick_name" id="account_nick_name" value="<?php echo get_user_meta( get_current_user_id(), 'nickname', true ); ?>"/>
</p>
<div class="clear"></div>

Now you will see that the field has been added BUT the code to validate/save the value of the field, you must also add yourself.

So for validation and saving add the following code to functions.php (or the way you prefer to add snippets)

// Validate - my account
function action_woocommerce_save_account_details_errors( $args ){
    if ( isset( $_POST['account_nick_name'] ) && empty( $_POST['account_nick_name'] ) ) {
        $args->add( 'error', __( 'Please provide a nick name', '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_nick_name'] ) && ! empty( $_POST['account_nick_name'] ) ) {
        update_user_meta( $user_id, 'nickname', sanitize_text_field( $_POST['account_nick_name'] ) );
    }
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • Thanks for your answer, your sympathy and your help for my first post on stackoverflow ! With your modifications (I was already using a custom template in yourtheme/woocommerce/myaccount/) now it works perfect !! Best, – David Arrouch Jan 16 '21 at 11:18