Skip to content

Instantly share code, notes, and snippets.

@gaiqus
Created December 13, 2019 13:55
Show Gist options
  • Save gaiqus/4e3ee860e61f9667e911d01d816c020e to your computer and use it in GitHub Desktop.
Save gaiqus/4e3ee860e61f9667e911d01d816c020e to your computer and use it in GitHub Desktop.
/**
* Return TRUE if supplied tax ID is valid for supplied country.
*
* @param string $id Taxation ID, e.g. ATU99999999 for Austria.
* @param string $customer_country Country code.
*
* @return bool
*/
function is_eu_vat_number_valid( $id, $customer_country ) {
$id = strtoupper( $id );
$id = preg_replace( '/[ -,.]/', '', $id );
if ( strlen( $id ) < 8 ) {
return false;
}
$country = substr( $id, 0, 2 );
$woocommerce_default_country = get_option( 'woocommerce_default_country', 0 );
if ( ! in_array( $customer_country, WC()->countries->get_european_union_countries() ) ) {
$id = $customer_country . $id;
$country = $customer_country;
} else {
if ( $woocommerce_default_country === $customer_country ) {
if ( $country != $customer_country ) {
$id = $customer_country . $id;
$country = $customer_country;
}
}
}
if ( $country !== $customer_country ) {
return false;
}
switch ( $country ) {
case 'AT': // AUSTRIA
$isValid = (bool) preg_match( '/^(AT)U(\d{8})$/', $id );
break;
case 'BE': // BELGIUM
$isValid = (bool) preg_match( '/(BE)(0?\d{9})$/', $id );
break;
case 'BG': // BULGARIA
$isValid = (bool) preg_match( '/(BG)(\d{9,10})$/', $id );
break;
case 'CHE': // Switzerland
$isValid = (bool) preg_match( '/(CHE)(\d{9})(MWST)?$/', $id );
break;
case 'CY': // CYPRUS
$isValid = (bool) preg_match( '/^(CY)([0-5|9]\d{7}[A-Z])$/', $id );
break;
case 'CZ': // CZECH REPUBLIC
$isValid = (bool) preg_match( '/^(CZ)(\d{8,10})(\d{3})?$/', $id );
break;
case 'DE': // GERMANY
$isValid = (bool) preg_match( '/^(DE)([1-9]\d{8})/', $id );
break;
case 'DK': // DENMARK
$isValid = (bool) preg_match( '/^(DK)(\d{8})$/', $id );
break;
case 'EE': // ESTONIA
$isValid = (bool) preg_match( '/^(EE)(10\d{7})$/', $id );
break;
case 'EL': // GREECE
$isValid = (bool) preg_match( '/^(EL)(\d{9})$/', $id );
break;
case 'ES': // SPAIN
$isValid = (bool) preg_match( '/^(ES)([A-Z]\d{8})$/', $id )
|| preg_match( '/^(ES)([A-H|N-S|W]\d{7}[A-J])$/', $id )
|| preg_match( '/^(ES)([0-9|Y|Z]\d{7}[A-Z])$/', $id )
|| preg_match( '/^(ES)([K|L|M|X]\d{7}[A-Z])$/', $id );
break;
case 'EU': // EU type
$isValid = (bool) preg_match( '/^(EU)(\d{9})$/', $id );
break;
case 'FI': // FINLAND
$isValid = (bool) preg_match( '/^(FI)(\d{8})$/', $id );
break;
case 'FR': // FRANCE
$isValid = (bool) preg_match( '/^(FR)(\d{11})$/', $id )
|| preg_match( '/^(FR)([(A-H)|(J-N)|(P-Z)]\d{10})$/', $id )
|| preg_match( '/^(FR)(\d[(A-H)|(J-N)|(P-Z)]\d{9})$/', $id )
|| preg_match( '/^(FR)([(A-H)|(J-N)|(P-Z)]{2}\d{9})$/', $id );
break;
case 'GB': // GREAT BRITAIN
$isValid = (bool) preg_match( '/^(GB)?(\d{9})$/', $id )
|| preg_match( '/^(GB)?(\d{12})$/', $id )
|| preg_match( '/^(GB)?(GD\d{3})$/', $id )
|| preg_match( '/^(GB)?(HA\d{3})$/', $id );
break;
case 'GR': // GREECE
$isValid = (bool) preg_match( '/^(GR)(\d{8,9})$/', $id );
break;
case 'HR': // CROATIA
$isValid = (bool) preg_match( '/^(HR)(\d{11})$/', $id );
break;
case 'HU': // HUNGARY
$isValid = (bool) preg_match( '/^(HU)(\d{8})$/', $id );
break;
case 'IE': // IRELAND
$isValid = (bool) preg_match( '/^(IE)(\d{7}[A-W])$/', $id )
|| preg_match( '/^(IE)([7-9][A-Z\*\+)]\d{5}[A-W])$/', $id )
|| preg_match( '/^(IE)(\d{7}[A-W][AH])$/', $id );
break;
case 'IT': // ITALY
$isValid = (bool) preg_match( '/^(IT)(\d{11})$/', $id );
break;
case 'LV': // LATVIA
$isValid = (bool) preg_match( '/^(LV)(\d{11})$/', $id );
break;
case 'LT': // LITHUNIA
$isValid = (bool) preg_match( '/^(LT)(\d{9}|\d{12})$/', $id );
break;
case 'LU': // LUXEMBOURG
$isValid = (bool) preg_match( '/^(LU)(\d{8})$/', $id );
break;
case 'MT': // MALTA
$isValid = (bool) preg_match( '/^(MT)([1-9]\d{7})$/', $id );
break;
case 'NL': // NETHERLAND
$isValid = (bool) preg_match( '/^(NL)(\d{9})B\d{2}$/', $id );
break;
case 'NO': // NORWAY
$isValid = (bool) preg_match( '/^(NO)(\d{9})$/', $id );
break;
case 'PL': // POLAND
$isValid = (bool) preg_match( '/^(PL)(\d{10})$/', $id );
break;
case 'PT': // PORTUGAL
$isValid = (bool) preg_match( '/^(PT)(\d{9})$/', $id );
break;
case 'RO': // ROMANIA
$isValid = (bool) preg_match( '/^(RO)([1-9]\d{1,9})$/', $id );
break;
case 'RS': // SERBIA
$isValid = (bool) preg_match( '/^(RS)(\d{9})$/', $id );
break;
case 'SI': // SLOVENIA
$isValid = (bool) preg_match( '/^(SI)([1-9]\d{7})$/', $id );
break;
case 'SK': // SLOVAK REPUBLIC
$isValid = (bool) preg_match( '/^(SK)([1-9]\d[(2-4)|(6-9)]\d{7})$/', $id );
break;
case 'SE': // SWEDEN
$isValid = (bool) preg_match( '/^(SE)(\d{10}01)$/', $id );
break;
default:
$isValid = false;
}
return $isValid;
}
/**
* Validates EU VAT number
*
* @param string $field_label Field label.
* @param string $value Value.
*/
function wpdesk_fcf_validate_eu_vat_number( $field_label, $value ) {
$valid = true;
$customer_country = WC()->customer->get_billing_country();
if ( in_array( $customer_country, WC()->countries->get_european_union_countries() ) ) {
$valid = is_eu_vat_number_valid( $value, $customer_country );
}
if ( false === $valid ) {
wc_add_notice( sprintf( __( 'Incorrect %1$s.', 'wpdesk' ), '<strong>' . $field_label . '</strong>' ), 'error' );
}
}
/**
* Own EU Vat number validation for WP Desk Flexible Checkout Fields Plugin
*
* @param array $custom_validation .
*
* @return array
*/
function wpdesk_fcf_custom_validation_nip( $custom_validation ) {
$custom_validation['eu_vat_number'] = array(
'label' => __( 'EU VAT number', 'wpdesk' ),
'callback' => 'wpdesk_fcf_validate_eu_vat_number'
);
return $custom_validation;
}
add_filter( 'flexible_checkout_fields_custom_validation', 'wpdesk_fcf_custom_validation_nip' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment