Home > Software engineering >  Add custom WooCommerce registration fields to admin WordPress user contact fields
Add custom WooCommerce registration fields to admin WordPress user contact fields

Time:10-29

I customized the user registration process of WooCommerce for this I added some extra fields like date of birth & Mobile number.

I added the form elements like this:

function my_extra_register_fields() {?>
    <p class="form-row form-row-wide">
    <label for="reg_dob"><?php _e( 'Date of Birth', 'woocommerce' ); ?><span class="required">*</span></label>
    <input type="text" class="input-text" name="reg_customer_dob" id="reg_customer_dob"  />
    </p>
    
 
    <p class="form-row form-row-wide">
    <label for="reg_billing_phone"><?php _e( 'Mobile', 'woocommerce' ); ?><span class="required">*</span></label>
    <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone"  />
    </p>

   <div class="clear"></div>
    <?php
}
add_action( 'woocommerce_register_form', 'my_extra_register_fields' );

and I am storing data like this -

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['reg_customer_dob'] ) ) {
        // Phone input filed which is used in WooCommerce
        update_user_meta( $customer_id, 'customer_dob', sanitize_text_field( $_POST['reg_customer_dob'] ) );
       }           
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

Now I am trying to add/show these two details (date of birth & Mobile number) in Edit user page of user menu so that admin can see the details. image for location is attached

enter image description here

But i am unable to read data in this page.


I am try to using the following hook:

add_action( 'edit_user_profile', 'print_custom_fileds_in_edit_user', 30 ); 

Any adivce?

CodePudding user response:

To display the field between the contact information you can use the user_contactmethods filter hook opposite edit_user_profile

You just need to make sure the user meta key matches

/**
 * Filters the user contact methods.
 *
 * @since 2.9.0
 *
 * @param string[] $methods Array of contact method labels keyed by contact method.
 * @param WP_User  $user    WP_User object.
 */
function filter_user_contactmethods( $methods, $user ) {    
    // Add user contact methods
    $methods['customer_dob']   = __( 'Date of Birth', 'your-theme-slug' );
    
    return $methods;
}
add_filter( 'user_contactmethods', 'filter_user_contactmethods', 10, 2 );
  • Related