This simple snippet makes the phone number field required during WooCommerce checkout. Customers won’t be able to complete their order without providing a phone number, and the snippet includes basic validation to ensure the phone number format is correct.
What does this code do?
Makes the billing phone field required (shows red asterisk *)
Validates that phone field is not empty during checkout
Validates phone number format (allows numbers, spaces, dashes, +, parentheses)
Ensures minimum 10 digits in the phone number
Shows error messages if validation fails
Perfect for:
Delivery services that need customer contact
Businesses requiring phone verification
Local stores needing customer phone numbers
Services requiring appointment scheduling
Emergency contact requirements
The validation is flexible and accepts common phone number formats like: 123-456-7890, (123) 456-7890, +1-123-456-7890, etc.
/**
* Make phone field required in WooCommerce checkout
* Simple validation for phone number
* Prefix: tpsc_ (TP Snippet Collection)
*/
// Make billing phone required
add_filter('woocommerce_billing_fields', 'tpsc_make_phone_required');
function tpsc_make_phone_required($fields) {
$fields['billing_phone']['required'] = true;
return $fields;
}
// Add validation for phone field
add_action('woocommerce_checkout_process', 'tpsc_validate_phone_field');
function tpsc_validate_phone_field() {
if (empty($_POST['billing_phone'])) {
wc_add_notice(__('Phone number is required.', 'woocommerce'), 'error');
}
}
// Optional: Add phone validation (numbers and basic characters only)
add_action('woocommerce_checkout_process', 'tpsc_validate_phone_format');
function tpsc_validate_phone_format() {
if (!empty($_POST['billing_phone'])) {
$phone = sanitize_text_field($_POST['billing_phone']);
// Allow numbers, spaces, dashes, plus sign, and parentheses
if (!preg_match('/^[\d\s\-\+\(\)]+$/', $phone)) {
wc_add_notice(__('Please enter a valid phone number.', 'woocommerce'), 'error');
}
// Check minimum length (at least 10 digits)
$digits_only = preg_replace('/[^\d]/', '', $phone);
if (strlen($digits_only) < 10) {
wc_add_notice(__('Phone number must be at least 10 digits.', 'woocommerce'), 'error');
}
}
}