Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.