0

I needed to add a field for phone number in the registration form of my woocommerce website.

I used the following code for it

function wooc_extra_register_fields() {
?>
  <div class="clear"></div>
  <p class="form-row form-row-wide">
    <label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?><span 
    class="required">*</span></label>
    <input type="text" class="input-text" name="billing_phone" 
    id="reg_billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] 
    ) 
    ) esc_attr_e( $_POST['billing_phone'] ); ?>" />
  </p>
  <?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' 
);


function wooc_validate_extra_register_fields( $username, $email, 
$validation_errors ) {
  if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) 
  {
    $validation_errors->add( 'billing_phone_error', __( '<strong>Error</strong>: 
    Phone is required!.', 'woocommerce' ) );
  }
}
add_action( 'woocommerce_register_post','wooc_validate_extra_register_fields', 10, 3 );


function wooc_save_extra_register_fields( $customer_id ) {
  if ( isset( $_POST['billing_phone'] ) ) {
  // WooCommerce billing phone
    update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( 
    $_POST['billing_phone'] ) );
  }
}
add_action( 'woocommerce_created_customer','wooc_save_extra_register_fields' );

The code is working perfectly and the new field is appearing in the registration form. However, the phone number is not getting saved in users of the admin panel. How do I make it display there?

Thanks

1 Answer 1

0

Try WC AFOUR, I had a similar requirement where I needed to add name & address fields to the WooCommerce registration form and it did exactly what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.