How to Add a checkout birth date required field with adult control in Woocommerce

The following code adds a billing birth date field and will check customer age avoiding checkout if customer is not at least 18 year old:

[php]// Adding a custom checkout date field
add_filter( ‘woocommerce_billing_fields’, ‘add_birth_date_billing_field’, 20, 1 );
function add_birth_date_billing_field($billing_fields) {

$billing_fields[‘billing_birth_date’] = array(
‘type’ => ‘date’,
‘label’ => __(‘Birth date’),
‘class’ => array(‘form-row-wide’),
‘priority’ => 25,
‘required’ => true,
‘clear’ => true,
);
return $billing_fields;
}

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

// Checking age and display an error notice avoiding checkout (and emptying cart)
if( $age < 18 ){
wc_add_notice( __( "You need at least to be 18 years old, to be able to checkout." ), "error" );

WC()->cart->empty_cart(); // <== Empty cart (optional)
}
}
}[/php]

Code goes in the functions.php file of your active child theme (or active theme). Tested and works.

Leave a Comment