How do I create a date of birth field with adult control in checkout?

// Display Billing birthdate field to checkout and My account addresses
add_filter( 'woocommerce_billing_fields', 'display_birthdate_billing_field', 20, 1 );
function display_birthdate_billing_field($billing_fields) {

    $billing_fields['billing_birthdate'] = array(
        'type'        => 'date',
        'label'       => __('Geburtsdatum'),
        'class'       => array('form-row-wide'),
        'priority'    => 25,
        'required'    => true,
        'clear'       => true,
    );
    return $billing_fields;
}

// Save Billing birthdate field value as user meta data
add_action( 'woocommerce_checkout_update_customer', 'save_account_billing_birthdate_field', 10, 2 );
function save_account_billing_birthdate_field( $customer, $data ){
    if ( isset($_POST['billing_birthdate']) && ! empty($_POST['billing_birthdate']) ) {
         $customer->update_meta_data( 'billing_birthdate', sanitize_text_field($_POST['billing_birthdate']) );
    }
}

// Admin orders Billing birthdate editable field and display
add_filter('woocommerce_admin_billing_fields', 'admin_order_billing_birthdate_editable_field');
function admin_order_billing_birthdate_editable_field( $fields ) {
    $fields['birthdate'] = array( 'label' => __('Geburtsdatum', 'woocommerce') );

    return $fields;
}

// WordPress User: Add Billing birthdate editable field
add_filter('woocommerce_customer_meta_fields', 'wordpress_user_account_billing_birthdate_field');
function wordpress_user_account_billing_birthdate_field( $fields ) {
    $fields['billing']['fields']['billing_birthdate'] = array(
        'label'       => __('Geburtsdatum', 'woocommerce'),
        'description' => __('', 'woocommerce')
    );
    return $fields;
}

// Check customer age
add_action('woocommerce_checkout_process', 'check_birth_date');
function check_birth_date() {
    // Check billing field
    if( isset($_POST['billing_birthdate']) && ! empty($_POST['billing_birthdate']) ){
        // Get customer age from birthdate
        $age = date_diff(date_create($_POST['billing_birthdate']), date_create('now'))->y;

        // Checking age and display an error notice avoiding checkout (and emptying cart)
        if( $age < 18 ){
            wc_add_notice( __( "Du musst mindestens 18 Jahre alt sein, um in unserem Shop bestellen zu können!" ), "error" );

            WC()->cart->empty_cart();
        }
    }
}
Without cookies
This website does not use cookies or tracking. More information can be found in the privacy policy.
  • Developed 2023
  • CMS WordPress
  • Status Completed

Just a Web-Developer

Creating a developer’s own website is always a big challenge. You want to integrate all the cool features and show all your skills. Therefore, it is rather called here to make cutbacks and to put the information in the foreground. I hope this is halfway successful here.
To add value, I’ve taken the „Helpful Code Snippets“ element form my old website and integrated a search function for them.

  • Developed 2022
  • CMS WordPress
  • Status Completed

Tatort Ruhrpott

A few years ago I had the idea for a photo project. I wanted to photograph crime scenes from the Ruhr region and show how these places look today. During the Corona Pandemic 2021, I started taking the photos and at the beginning of 2022, I started implementing the corresponding website.

  • Developed 2018
  • CMS WordPress
  • Status Completed

CMS Geek

CMS Geek was a website project that I started in 2018 and was supposed to introduce users to the functions of WordPress through explanatory videos. In addition to a blog that informed about the latest news about WordPress and Joomla, there were also helpful code solutions to common WordPress, html and CSS problems.

Due to the lack of time to produce more explainer videos, I discontinued this project in spring 2023. I have taken over the code snippets for Just-a-Web-Developer, updated them and will continue to expand them.