WP-Members

Add date-of-birth field to my-account / account detail page

/* add date of birth field account detail page */
// Add field – my account
function action_woocommerce_edit_account_form() {
woocommerce_form_field( ‘date_of_birth’, array(
‘type’ => ‘date’,
‘label’ => ( ‘My Birth Date’, ‘woocommerce’ ), ‘placeholder’ => ( ‘Date of Birth’, ‘woocommerce’ ),
‘required’ => true,
), get_user_meta( get_current_user_id(), ‘date_of_birth’, true ));
}
add_action( ‘woocommerce_edit_account_form’, ‘action_woocommerce_edit_account_form’ );
// Validate – my account
function action_woocommerce_save_account_details_errors( $args ){
if ( isset($_POST[‘date_of_birth’]) && empty($_POST[‘date_of_birth’]) ) {
$args->add( ‘error’, __( ‘Please provide a birth date’, ‘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[‘date_of_birth’]) && ! empty($_POST[‘date_of_birth’]) ) {
update_user_meta( $user_id, ‘date_of_birth’, sanitize_text_field($_POST[‘date_of_birth’]) );
}
}

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *