Skip to content

Instantly share code, notes, and snippets.

@eimkasp
Created February 8, 2024 11:22
Show Gist options
  • Save eimkasp/3c8ea224e70712f41af0b2f270bf007e to your computer and use it in GitHub Desktop.
Save eimkasp/3c8ea224e70712f41af0b2f270bf007e to your computer and use it in GitHub Desktop.
WooCommerce Phone Number Validation (+370/+371/+372) for LT/LV/EE
// Add following code to functions php of your theme
add_action( 'woocommerce_checkout_process', 'validate_phone_number_for_specific_countries' );
function validate_phone_number_for_specific_countries() {
$phone = isset( $_POST['billing_phone'] ) ? wc_clean( $_POST['billing_phone'] ) : '';
$billing_country = isset( $_POST['billing_country'] ) ? wc_clean( $_POST['billing_country'] ) : '';
// Specify the countries requiring phone number validation
$countries_requiring_validation = array( 'LT', 'LV', 'EE' );
// Proceed with validation if the billing country is one of the specified countries
if ( in_array( $billing_country, $countries_requiring_validation ) ) {
// Check if the phone number starts with '+' followed by digits
if ( !preg_match( '/^\+\d+/', $phone ) ) {
wc_add_notice( __( 'For Lithuania, Latvia, and Estonia, please enter a valid phone number with your country code. For example, +37012345678.', 'woocommerce' ), 'error' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment