/* Add field to my account detail */
// 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']) );
    }
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );

By admin

Leave a Reply

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