AI Verified

Name

WooCommerce Custom Date Field On Registration Form

About

This guide shows you how you can implement a custom date picker field on the My Account registration form in WooCoomerce and save the data as User Meta shown in User Profile Editor in the Dashboard. The date is saved to the database. We’ve also implemented two validation methods. This particular guide asks for the user’s date of birth which is saved as user meta. We’ve made the field required so it must be entered. An error will show if the custom date of birth field is left blank. An error will also show if the user enters a date of birth later than the year of 2005.

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

Simply copy the pre-coded solution to your active child theme’s functions.php or preferably the Code Snippets Plugin.

Link for further information:

The author has provided the following URL that may be helpful to setting up or using this snippet:

https://www.ecommercehints.com/woocommerce-custom-date-field-on-registration-form

Codevault

ecommercehints

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

Website/ Profile URL:

https://www.ecommercehints.com/

History

Last modified:

15/11/2022

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

WooCommerce Custom Date Field On Registration Form

 
                    
1/**
2 * Snippet Name: WooCommerce Date Of Birth Field On Registration Form
3 * Snippet Author: ecommercehints.com
4 */
5 
6// Create the new Date Of Birth field
7add_action( 'woocommerce_register_form', 'ecommercehints_register_date_form_field' );
8function ecommercehints_register_date_form_field(){
9 woocommerce_form_field(
10 'date_of_birth',
11 array(
12 'type' => 'date',
13 'required' => true, // Shows an asterisk if true (*)
14 'label' => 'Date Of Birth'
15 ),
16 ( isset($_POST['date_of_birth']) ? $_POST['date_of_birth'] : '' )
17 );
18}
19 
20// Show an error if Date Of Birth is not filled out when registering
21add_action( 'woocommerce_register_post', 'ecommercehints_validate_dob', 10, 3 );
22function ecommercehints_validate_dob( $username, $email, $errors ) {
23 if ( empty( $_POST['date_of_birth'] ) ) {
24 $errors->add( 'date_of_birth_error', 'We need to store this information to send you a birthday card.' );
25 }
26 if($_POST['date_of_birth'] > 2005) {
27 $errors->add( 'date_of_birth_error', 'You must be born in 2006 or earlier' );
28 }
29}
30 
31// Save the Date Of Birth field as User Meta
32add_action( 'woocommerce_created_customer', 'ecommercehints_save_dob_field' );
33function ecommercehints_save_dob_field( $customer_id ){
34 if ( isset( $_POST['date_of_birth'] ) ) {
35 update_user_meta( $customer_id, 'date_of_birth', wc_clean( date("d/m/Y", strtotime($_POST['date_of_birth'])) ) );
36 }
37}
38 
39// Show Date Of Birth field in the User Editor
40add_action('show_user_profile', 'custom_user_profile_fields');
41add_action('edit_user_profile', 'custom_user_profile_fields');
42 
43function custom_user_profile_fields( $user ) { ?>
44<h2>Custom user meta fields</h2>
45 <table class="form-table">
46 <tr>
47 <th>
48 <label for="date_of_birth"><?php _e( 'Date Of Birth' ); ?></label>
49 </th>
50 <td>
51 <input type="text" name="date_of_birth" id="date_of_birth" value="<?php echo esc_attr( get_the_author_meta( 'date_of_birth', $user->ID ) ); ?>" class="regular-text" />
52 </td>
53 </tr>
54 </table>
55<?php
56}
57 
58// Save new Date Of Birth if edited by admin
59add_action( 'personal_options_update', 'update_extra_profile_fields' );
60add_action( 'edit_user_profile_update', 'update_extra_profile_fields' );
61function update_extra_profile_fields( $user_id ) {
62 if ( current_user_can( 'edit_user', $user_id ) )
63 update_user_meta( $user_id, 'date_of_birth', date("d/m/Y", strtotime($_POST['date_of_birth'])) );
64}

0

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

1

WooCommerce Change “Choose Some Product Options” Text

Added: 1 year ago

Last Updated: 2 months ago

A lot of the time, users will miss the attribute drop down on a variable product page and immediately go for the add to cart button. If this happens, and the user has not yet selected an option from t...

WooCommerce

AI Verified

1

WooCommerce Show Product Unit Price

Added: 1 year ago

Last Updated: 2 months ago

By default, WooCommerce only shows the product’s unit price (or price per item) only on the cart page. On the Checkout, Thank You page, Emails, and order view in My Account, the user is not shown the...

WooCommerce

AI Verified

1

WooCommerce Add Custom Checkout Field If Specific Product Is In Cart

Added: 1 year ago

Last Updated: 2 months ago

This snippet will show a custom checkout field and save the data as order meta ONLY IF a specific product is in the cart.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WooCommerce

AI Verified

7

WooCommerce Show Number Of People Viewing Product

Added: 1 year ago

Last Updated: 1 month ago

We want to make this solution absolutely clear from the outset. This snippet does NOT generate the actual number of people viewing the product. It shows a randomly generated number in the range of two...

WooCommerce

AI Verified

5

WooCommerce Show Number Of Units Sold On Product Page

Added: 1 year ago

Last Updated: 2 months ago

Showing the number of product units sold on the single product page is a great way to introduce Fear Of Missing Out (FOMO). You can highlight how many products are sold to let customers know a product...

WooCommerce

AI Verified

3

WooCommerce Add Content Under The Proceed To Checkout Button On The Cart Page

Added: 1 year ago

Last Updated: 2 months ago

How many times have you bought something online and seen trust symbols near the proceed to checkout button on the cart page? “30 Day Money Back Guarantee” or “Secure Checkout” are popular examples. By...