Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

Check Greek phone numbers at checkout

For customers in Greece, the checkout accepts only valid Greek phone numbers: 10 digits starting with 2 (landline) or 69 (mobile), with or without +30.

  • Type PHP
  • Runs Everywhere (runs as WordPress loads)
  • Needs WooCommerce
  • Category Greek e-shop
  • Tested with WordPress 7.1, WooCommerce 11.1
PHP
<?php

// 10 digits starting with 2 (landline) or 69 (mobile); +30 or 0030 in front is fine.
$valid_phone = static function ( string $phone ): bool {
	$phone = preg_replace( '/[\s\-().]/', '', $phone );
	$phone = preg_replace( '/^(\+30|0030)/', '', $phone );
	return (bool) preg_match( '/^(2\d{9}|69\d{8})$/', $phone );
};
$message = 'Ο αριθμός τηλεφώνου δεν είναι έγκυρος. Γράψτε 10 ψηφία, π.χ. 6912345678 ή 2310123456.';

// Classic checkout.
add_action(
	'woocommerce_after_checkout_validation',
	static function ( $data, $errors ) use ( $valid_phone, $message ) {
		if ( 'GR' !== ( $data['billing_country'] ?? '' ) || '' === (string) ( $data['billing_phone'] ?? '' ) ) {
			return;
		}
		if ( ! $valid_phone( (string) $data['billing_phone'] ) ) {
			$errors->add( 'validation', $message );
		}
	},
	10,
	2
);

// Block checkout: billing and shipping addresses.
add_action(
	'woocommerce_blocks_validate_location_address_fields',
	static function ( $errors, $fields, $group ) use ( $valid_phone, $message ) {
		if ( 'GR' !== ( $fields['country'] ?? '' ) || '' === (string) ( $fields['phone'] ?? '' ) ) {
			return;
		}
		if ( ! $valid_phone( (string) $fields['phone'] ) ) {
			$errors->add( 'snippet_gr_phone_' . $group, $message );
		}
	},
	10,
	3
);

Good to know

Works with the classic checkout and the block checkout (WooCommerce 8.9 or later), for billing and shipping phones.

In these packs

  • Greek e-shop: For shops in Greece: receipt or invoice with a checked VAT number (ΑΦΜ), Greek phone and postcode checks, a cash on delivery fee, and greeklish addresses.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.