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' );
}
}