25% off ProSNIPC25OFF

Add Custom Terms Checkbox at Checkout

Adds a required Terms and Conditions acceptance checkbox before the Place Order button on the WooCommerce checkout page and validates it server-side.

PHPby SnipCraft
php
<?php
if ( ! class_exists( 'WooCommerce' ) ) {
    return;
}

add_action( 'woocommerce_review_order_before_submit', 'scseed_checkout_terms_checkbox' );
function scseed_checkout_terms_checkbox() {
    $terms_url = function_exists( 'wc_get_page_permalink' ) ? wc_get_page_permalink( 'terms' ) : home_url( '/terms/' );
    woocommerce_form_field( 'scseed_accept_terms', array(
        'type'     => 'checkbox',
        'class'    => array( 'form-row-wide' ),
        'label'    => sprintf(
            /* translators: %s: Terms and Conditions URL */
            __( 'I have read and agree to the <a href="%s" target="_blank" rel="noopener noreferrer">Terms &amp; Conditions</a>.', 'woocommerce' ),
            esc_url( $terms_url )
        ),
        'required' => true,
    ), WC()->checkout->get_value( 'scseed_accept_terms' ) );
}

add_action( 'woocommerce_checkout_process', 'scseed_checkout_terms_validation' );
function scseed_checkout_terms_validation() {
    if ( empty( $_POST['scseed_accept_terms'] ) ) {
        wc_add_notice( __( 'Please accept the Terms &amp; Conditions to place your order.', 'woocommerce' ), 'error' );
    }
}
#checkout#compliance#terms#validation#woocommerce