skip to Main Content

WordPress Code Snippets


Adding a Custom Checkbox to the WooCommerce Checkout Page

Implement this code snippet to add a required checkbox at checkout, ensuring customers acknowledge your terms before completing their purchase.

// Add a custom checkbox to the checkout page
add_action( 'woocommerce_review_order_before_submit', 'add_custom_checkout_checkbox' );
function add_custom_checkout_checkbox() {
    echo '<div class="custom-checkbox">
        <input type="checkbox" id="custom_checkbox" name="custom_checkbox" />
        <label for="custom_checkbox">I agree to the terms and conditions</label>
    </div>';
}

// Validate the checkbox
add_action( 'woocommerce_checkout_process', 'validate_custom_checkout_checkbox' );
function validate_custom_checkout_checkbox() {
    if ( ! isset( $_POST['custom_checkbox'] ) ) {
        wc_add_notice( __( 'You must agree to the terms and conditions.' ), 'error' );
    }
}

I’m a WordPress developer with 10+ years of experience in WooCommerce and custom plugins. I combine technical expertise with design flair to help you create standout, user-friendly websites. Let’s transform your digital presence!

Best WordPress Hosting
Back To Top
Search