Home > Back-end >  How to change the added phone value in edit-account template form?
How to change the added phone value in edit-account template form?

Time:11-13

I needed to add fields 'firstname','lastname' and 'phone' to the register woocommerce form. I've found that decision.

function wooc_extra_register_fields() {?>
         <div class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide form-group">           
           <input type="text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" class="woocommerce-Input woocommerce-Input--text input-text form-control" size="25" placeholder="<?php _e( 'First Name', 'mydomain' ) ?>" />
        </div>
        <div class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide form-group">       
            <input type="text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" placeholder="<?php _e( 'Last Name', 'mydomain' ) ?>" class="woocommerce-Input woocommerce-Input--text input-text form-control" size="25" />
        </div>

         <div class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide form-group">       
            <input type="text" name="billing_phone" id="reg_billing_phone" placeholder="<?php _e( 'Phone', 'woocommerce' ); ?>" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" class="woocommerce-Input woocommerce-Input--text input-text form-control" size="25" />
        </div>
 
       <?php
 }
 add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );

 /**

* register fields Validating.

*/

function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {

      if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {

             $validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );

      }

      if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {

             $validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );

      }


      if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {

             $validation_errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Phone is required!.', 'woocommerce' ) );

      }

         return $validation_errors;
}

add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
/**
* Below code save extra fields.
*/
function wooc_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST['billing_phone'] ) ) {
                 // Phone input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
          }
      if ( isset( $_POST['billing_first_name'] ) ) {
             //First name field which is by default
             update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
             // First name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
      }
      if ( isset( $_POST['billing_last_name'] ) ) {
             // Last name field which is by default
             update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
             // Last name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );      }

}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' ); 

In myaccount form-edit-account.php template it looks like that

<form class="woocommerce-EditAccountForm edit-account" action="" method="post" <?php do_action( 'woocommerce_edit_account_form_tag' ); ?> >

   <?php do_action( 'woocommerce_edit_account_form_start' ); ?>

   <p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first page-inside-p">
       <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="account_first_name" id="account_first_name" autocomplete="given-name" value="<?php echo esc_attr( $user->first_name ); ?>" placeholder="<?php esc_html_e( 'First name', 'woocommerce' ); ?>" />
   </p>
   <p class="woocommerce-form-row woocommerce-form-row--last form-row form-row-last page-inside-p">        
       <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="account_last_name" id="account_last_name" autocomplete="family-name" value="<?php echo esc_attr( $user->last_name ); ?>" placeholder="<?php esc_html_e( 'Last name', 'woocommerce' ); ?>" />
   </p>
   <div class="clear"></div>

   <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide page-inside-p">        
       <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="account_display_name" id="account_display_name" value="<?php echo esc_attr( $user->display_name ); ?>" placeholder="<?php esc_html_e( 'Display name', 'woocommerce' ); ?>" /> 
   </p>
   <div class="clear"></div>

   <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide mb-20 page-inside-p">      
       <input type="email" class="woocommerce-Input woocommerce-Input--email input-text form-control" name="account_email" id="account_email" autocomplete="email" value="<?php echo esc_attr( $user->user_email ); ?>" placeholder="<?php esc_html_e( 'Email address', 'woocommerce' ); ?>" />
   </p>

        <div class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide form-group">       
           <input type="text" name="billing_phone" id="reg_billing_phone" placeholder="<?php _e( 'Phone', 'woocommerce' ); ?>" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" class="woocommerce-Input woocommerce-Input--text input-text form-control" size="25" />
       </div>....etc

The first name and the last name are can be changed correctly, but I don't see the value of a phone number in that page and can't update it to save. What's the right way to do it? I copied the same phone field as in the register form, but it seems that's a wrong way. How to display and change it correctly?

CodePudding user response:

The first part of your code is for updating user fields. You have "name" attributes in your HTML Input tags. Submitting these values are achieved by these names.

After submission, if those fields are validated then update_user_meta function is used to update database records for this user with new values of given fields. So, the $_POST['billing_phone'] is used for this purpose and you can not retrieve data by using it.

The problem is in your "myaccount form-edit-account.php". As you can see, all other correct parts gets values from user object. Like value="<?php echo esc_attr( $user->first_name ); ?>" gets the current user's first name.

For phone number, you should use billing_phone field since you are using update_user_meta method for this field and you can retrive this updated data by using get_user_meta method.

So, replacing value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" with correct assignment value="<?php echo esc_attr( get_user_meta($user->id,'billing_phone',true) ); ?>" will solve your problem.

Also, to update this phone number (billing_phone) field from my account page you may check this answer which is directly shows the solution: Add a custom field in Woocommerce Edit Account page

Hope this helps.

  • Related