Validate Greek phone numbers at checkout
Check that Greek billing phone numbers in WooCommerce have 10 digits and start with 2 or 69. Works with block and classic checkout.
// Check that Greek billing phone numbers have 10 digits and start with 2 (landline) or 69 (mobile).
// Accepts spaces, dashes and a +30 or 0030 prefix.
function snipfire_is_valid_greek_phone( $phone ) {
$digits = preg_replace( '/[^0-9]/', '', (string) $phone );
$digits = preg_replace( '/^(0030|30)(?=[0-9]{10}$)/', '', $digits );
return (bool) preg_match( '/^(2[0-9]{9}|69[0-9]{8})$/', $digits );
}
function snipfire_greek_phone_error() {
return 'Ο αριθμός τηλεφώνου πρέπει να έχει 10 ψηφία και να ξεκινά από 2 ή 69.';
}
// Classic (shortcode) checkout.
add_action(
'woocommerce_after_checkout_validation',
function ( $data, $errors ) {
if (
isset( $data['billing_country'], $data['billing_phone'] )
&& 'GR' === $data['billing_country']
&& '' !== $data['billing_phone']
&& ! snipfire_is_valid_greek_phone( $data['billing_phone'] )
) {
$errors->add( 'billing_phone_validation', snipfire_greek_phone_error() );
}
},
10,
2
);
// Block checkout (and the address form in My account).
add_action(
'woocommerce_blocks_validate_location_address_fields',
function ( $errors, $fields, $group ) {
if (
'billing' === $group
&& isset( $fields['country'], $fields['phone'] )
&& 'GR' === $fields['country']
&& '' !== $fields['phone']
&& ! snipfire_is_valid_greek_phone( $fields['phone'] )
) {
$errors->add( 'snipfire_invalid_greek_phone', snipfire_greek_phone_error() );
}
},
10,
3
);
What it does
Wrong phone numbers mean couriers can't reach your customers. Greek landlines have 10 digits starting with 2, and mobiles have 10 digits starting with 69.
This snippet checks the billing phone when the billing country is Greece. It accepts spaces, dashes and a +30 or 0030 prefix, and shows a message in Greek if the number doesn't match. It works in the block checkout, the classic checkout and the My account address form.
Good to know
- Only checks orders with a billing address in Greece, and only when a phone number has been entered.
- The error message is in Greek. Change the text in
snipfire_greek_phone_error()if your checkout is in another language.