0

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?

libertarian
  • 345
  • 1
  • 15

1 Answers1

1

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.

MrEbabi
  • 740
  • 5
  • 16
  • I replaced, and sadly this haven't helped – libertarian Nov 12 '21 at 21:28
  • It seems that the phone, that I write in register form is shoing fine, but it doesn't want to update. Or on the contrary - if not show, it updates. Don't know where is the conflict – libertarian Nov 12 '21 at 22:14
  • Maybe I do not understand the main problem. Can you specify it please? Is it about "register woocommerce form" or "myaccount form-edit-account.php"? The solution I wrote is for showing billing phone in the my account user details page. – MrEbabi Nov 12 '21 at 22:38
  • It's about myaccount form-edit-account.php. The phone from register form is showing correctly, but I can't change its value in myaccount page. it's here https://i83862.hostua5.fornex.host(the domain is technical). The register form is in popup, the account page is here https://i83862.hostua5.fornex.host/my-account/edit-account/ – libertarian Nov 12 '21 at 23:23
  • Ok I got it now. But I believe with your first code, you also do not able to show phone number in my account page, it should be shown correctly when you replace the value. For updating this field in my account page please try the accepted answer here: https://stackoverflow.com/questions/47599050/add-a-custom-field-in-woocommerce-edit-account-page – MrEbabi Nov 12 '21 at 23:29
  • somehow I fixed it. thanks. I tried many. The input is the same, but the actions are different for the order and the account page. I hope it works. not only seems like that – libertarian Nov 13 '21 at 00:17
  • I just checked your website, and changed my phone number using my account, it looks fine! – MrEbabi Nov 13 '21 at 00:21