WooCommerce : Custom validation on checkout page for First and Last names to Contain Only Letters.
To add custom validation on the WooCommerce checkout page for the first and last names to contain only letters, you can use the `woocommerce_after_checkout_validation` filter hook. This hook allows you to perform custom validation on the submitted checkout data before processing the order. Here’s how you can add the custom validation: 1. Open your theme’s `functions.php` file or a custom plugin file. 2. Add the following code to implement the validation: function custom_validate_checkout_names($fields, $errors) { // Validate First Name if (isset($_POST[‘billing_first_name’]) && !preg_match(‘/^[A-Za-z]+$/’, $_POST[‘billing_first_name’])) { $errors->add(‘billing_first_name’, __(‘First name must contain only letters.’, ‘text-domain’)); } // Validate Last Name if (isset($_POST[‘billing_last_name’]) && !preg_match(‘/^[A-Za-z]+$/’, $_POST[‘billing_last_name’])) { $errors->add(‘billing_last_name’, __(‘Last name must contain only letters.’, ‘text-domain’)); } return $fields; } add_filter(‘woocommerce_after_checkout_validation’, ‘custom_validate_checkout_names’, 10, 2); In this code, we use the `woocommerce_after_checkout_validation` filter hook to add our custom validation function `custom_validate_checkout_names`. The `custom_validate_checkout_names` function checks the submitted first and last names for containing only