How to add age field to WooCommerce My Account

Today we are going to talk about how to add an age field to the “My Account” page in WooCommerce. In order to add age field to my account, first you have to go to “myaccount/form-edit-account.php” which will be found in templates folder. Once you’re in replace the code from line 36 to line 40 with the following code:

<p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first">
    <label for="account_age"><?php esc_html_e( 'Age', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_age" id="account_age" value="<?php echo isset($user->age) ? esc_attr( $user->age ) : ''; ?>" />
</p>
<p class="woocommerce-form-row woocommerce-form-row--last form-row form-row-last">
    <label for="account_display_name"><?php esc_html_e( 'Display name', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_display_name" id="account_display_name" value="<?php echo esc_attr( $user->display_name ); ?>" /> <span><em><?php esc_html_e( 'This will be how your name will be displayed in the account section and in reviews', 'woocommerce' ); ?></em></span>
</p>
<div class="clear"></div>

Now in order to save age values in database, go to your active or child theme’s function.php file and add the following code:

add_action('woocommerce_save_account_details', 'qaisarsatti_save_account_details_age', 20, 1 );
function qaisarsatti_save_account_details_age( $user_id ) {
    if ( isset($_POST['account_age']) ) {
        update_user_meta($user_id, 'age', sanitize_text_field($_POST['account_age'] ) );
    }
}

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!