Skip to content

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

Check Greek postcodes at checkout

For addresses in Greece, the checkout accepts only valid 5-digit postcodes (10000 to 85999, with or without a space).

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

// Greek postcodes: 5 digits, 10000–89999 (a space after the third is fine).
$valid_postcode = static function ( string $postcode ): bool {
	return (bool) preg_match( '/^[1-8]\d{4}$/', preg_replace( '/\s+/', '', $postcode ) );
};
$message = 'Ο ταχυδρομικός κώδικας πρέπει να έχει 5 ψηφία, π.χ. 57001.';

// Classic checkout.
add_action(
	'woocommerce_after_checkout_validation',
	static function ( $data, $errors ) use ( $valid_postcode, $message ) {
		foreach ( array( 'billing', 'shipping' ) as $type ) {
			if ( 'shipping' === $type && empty( $data['ship_to_different_address'] ) ) {
				continue;
			}
			if ( 'GR' !== ( $data[ $type . '_country' ] ?? '' ) ) {
				continue;
			}
			$postcode = (string) ( $data[ $type . '_postcode' ] ?? '' );
			if ( '' !== $postcode && ! $valid_postcode( $postcode ) ) {
				$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_postcode, $message ) {
		$postcode = (string) ( $fields['postcode'] ?? '' );
		if ( 'GR' === ( $fields['country'] ?? '' ) && '' !== $postcode && ! $valid_postcode( $postcode ) ) {
			$errors->add( 'snippet_gr_postcode_' . $group, $message );
		}
	},
	10,
	3
);

Good to know

Works with the classic checkout and the block checkout (WooCommerce 8.9 or later).

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.