Created
February 8, 2024 11:22
-
-
Save eimkasp/3c8ea224e70712f41af0b2f270bf007e to your computer and use it in GitHub Desktop.
WooCommerce Phone Number Validation (+370/+371/+372) for LT/LV/EE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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