Custom Checkout Validation — a practical guide to WooCommerce checkout validation with clear examples you can reuse in real projects.
WooCommerce Plugin Development Series (41/101). Prefer one article? Read the complete WooCommerce plugin development tutorial.
Short description
Use `woocommerce_checkout_process` and `wc_add_notice(…, 'error')` to block checkout.
Validate GSTIN
add_action('woocommerce_checkout_process', function () {
if (empty($_POST['billing_gstin'])) {
return;
}
$gstin = sanitize_text_field(wp_unslash($_POST['billing_gstin']));
if (strlen($gstin) < 15) {
wc_add_notice('Enter a valid GSTIN.', 'error');
}
});