Age Confirmation Checkbox at Checkout
Adds a required "I confirm I am 18 or older" checkbox to the classic checkout and records the confirmation on the order.
Opens WPIDE on
Code
$wpide_min_age = 18; // Minimum age the customer must confirm.
add_action( 'woocommerce_review_order_before_submit', function () use ( $wpide_min_age ) {
woocommerce_form_field( 'wpide_age_confirm', array(
'type' => 'checkbox',
'class' => array( 'form-row', 'validate-required' ),
'label' => sprintf( 'I confirm that I am %d years or older.', $wpide_min_age ),
'required' => true,
) );
} );
add_action( 'woocommerce_checkout_process', function () {
if ( empty( $_POST['wpide_age_confirm'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
wc_add_notice( 'Please confirm your age to place the order.', 'error' );
}
} );
add_action( 'woocommerce_checkout_create_order', function ( $order ) use ( $wpide_min_age ) {
if ( ! empty( $_POST['wpide_age_confirm'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
$order->update_meta_data( '_wpide_age_confirmed', $wpide_min_age . '+ confirmed ' . current_time( 'mysql' ) );
}
} );
Notes
Change $age to your legal age. This is a self-declaration, not an identity check.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.