Skip to content

Instantly share code, notes, and snippets.

@Preciousomonze
Created July 29, 2021 09:16
Show Gist options
  • Save Preciousomonze/807168b53a53e4d7f07593659e8ebb23 to your computer and use it in GitHub Desktop.
Save Preciousomonze/807168b53a53e4d7f07593659e8ebb23 to your computer and use it in GitHub Desktop.
Temporary fix: conflict with some themes (JS error - Uncaught SyntaxError) #40: https://https://github.com/Preciousomonze/woocommerce-phone-validator/issues/40
jQuery( document ).ready( function( $ ){
// set phone number properly for intl
// here, the index maps to the error code returned from getValidationError
var wcPvPhoneErrorMap = wcPvJson.validationErrors;
// start
if ($( '.wc-pv-intl input' ).length == 0) {// add class, some checkout plugin has overriden my baby
$( '#billing_phone_field' ).addClass( 'wc-pv-phone wc-pv-intl' );
}
// Set default country.
var wcPvDefCountry = ( wcPvJson.defaultCountry == '' ? $( `${wcPvJson.parentPage} #billing_country` ).val() : wcPvJson.defaultCountry );
let separateDialCode = ( wcPvJson.separateDialCode == 1 ? true : false );
let onlyCountries = wcPvJson.onlyCountries.map( value => { return value.toUpperCase(); } );
var wcPvPhoneIntl = $( '.wc-pv-intl input' ).intlTelInput(
{
initialCountry: ( ( wcPvDefCountry == '' || wcPvDefCountry == undefined ) ? 'NG' : wcPvDefCountry ),
onlyCountries: onlyCountries,
separateDialCode: separateDialCode,
utilsScript: wcPvJson.utilsScript,
preferredCountry: '',
//autoHideDialCode: true,
//nationalMode: false,
/* geoIpLookup: function(callback) {
$.get('https://ipinfo.io', function() {}, "jsonp").always(function(resp) {
const countryCode = (resp && resp.country) ? resp.country : '';//asking for payment shaa,smh
callback(countryCode);
});
},//to pick user country */
}
);
/*if (wcPvJson.userPhone !== undefined ) {
wcPvPhoneIntl.intlTelInput("setNumber").val(wcPvJson.userPhone);
}*/
// Some Globals.
var wcPvphoneErrMsg = '';
/**
* Validates the phone number
*
* @param intlTelInput input
* @return string or bool
*/
function wcPvValidatePhone( input ) {
const phone = input;
let result = false;
if ( phone.intlTelInput( 'isValidNumber' ) == true ) {
result = phone.intlTelInput( 'getNumber' );
} else {
let errorCode = phone.intlTelInput( 'getValidationError' );
wcPvphoneErrMsg = `${wcPvJson.phoneErrorTitle + (wcPvPhoneErrorMap[errorCode] == undefined ? wcPvJson.phoneUnknownErrorMsg : wcPvPhoneErrorMap[errorCode])}`;
}
return result;
}
// Incase of country change
$( `${wcPvJson.parentPage} #billing_country` ).change(
function() {
let value = $( this ).val();
// Make sure you only set if its in the selected countries
if ( onlyCountries.includes( value ) ) {
wcPvPhoneIntl.intlTelInput( 'setCountry', value );
}
}
);
// Adjust design if true.
if ( separateDialCode === true ) {
$( '.wc-pv-intl' ).addClass( 'wc-pv-separate-dial-code' );
}
/**
* Js validation process
*
* @param {object} parentEl the parent element
*/
function wcPvValidateProcess( parentEl ) {
let phoneNumber = wcPvValidatePhone( wcPvPhoneIntl );
if ( $( '.wc-pv-intl input' ).length == 0) { // Doesnt exist, no need.
return;
}
// Remove errors first, so its not stagnant, special thanks to Sylvain :)
$( '#wc-ls-phone-valid-field-err-msg' ).remove();
if ( phoneNumber != false ) { // Phone is valid.
$( `${wcPvJson.parentPage} input#billing_phone` ).val( phoneNumber ); // Set the real value so it submits it along.
if ( $( '#wc-ls-phone-valid-field' ).length == 0 ) { // Append.
parentEl.append( ` < input id = "wc-ls-phone-valid-field" value = "${phoneNumber}" type = "hidden" name = "${wcPvJson.phoneValidatorName}" > ` );
}
} else {
parentEl.append( ` < input id = "wc-ls-phone-valid-field-err-msg" value = "${wcPvphoneErrMsg}" type = "hidden" name = "${wcPvJson.phoneValidatorErrName}" > ` );
$( '#wc-ls-phone-valid-field' ).remove();
}
}
// For woocommerce checkout.
if ( wcPvJson.currentPage == 'checkout' ) {
let wcPvCheckoutForm = $( `${wcPvJson.parentPage}` );
wcPvCheckoutForm.on(
'checkout_place_order',
function(){
wcPvValidateProcess( wcPvCheckoutForm );
}
);
} else if ( wcPvJson.currentPage == 'account') { // For account page.
let wcPvAccForm = $( `${wcPvJson.parentPage} form` );
$( `${wcPvJson.parentPage}` ).submit(
function(){
wcPvValidateProcess( wcPvAccForm );
}
);
}
} );
jQuery(document).ready(function(n){var e=wcPvJson.validationErrors;0==n(".wc-pv-intl input").length&&n("#billing_phone_field").addClass("wc-pv-phone wc-pv-intl");var t=""==wcPvJson.defaultCountry?n(`${wcPvJson.parentPage} #billing_country`).val():wcPvJson.defaultCountry;let l=1==wcPvJson.separateDialCode,i=wcPvJson.onlyCountries.map(n=>n.toUpperCase());var o=n(".wc-pv-intl input").intlTelInput({initialCountry:""==t||null==t?"NG":t,onlyCountries:i,separateDialCode:l,utilsScript:wcPvJson.utilsScript,preferredCountry:""}),a="";function r(t){let l=function(n){const t=n;let l=!1;if(1==t.intlTelInput("isValidNumber"))l=t.intlTelInput("getNumber");else{let n=t.intlTelInput("getValidationError");a=`${wcPvJson.phoneErrorTitle+(null==e[n]?wcPvJson.phoneUnknownErrorMsg:e[n])}`}return l}(o);0!=n(".wc-pv-intl input").length&&(n("#wc-ls-phone-valid-field-err-msg").remove(),0!=l?(n(`${wcPvJson.parentPage} input#billing_phone`).val(l),0==n("#wc-ls-phone-valid-field").length&&t.append(` < input id = "wc-ls-phone-valid-field" value = "${l}" type = "hidden" name = "${wcPvJson.phoneValidatorName}" > `)):(t.append(` < input id = "wc-ls-phone-valid-field-err-msg" value = "${a}" type = "hidden" name = "${wcPvJson.phoneValidatorErrName}" > `),n("#wc-ls-phone-valid-field").remove()))}if(n(`${wcPvJson.parentPage} #billing_country`).change(function(){let e=n(this).val();i.includes(e)&&o.intlTelInput("setCountry",e)}),!0===l&&n(".wc-pv-intl").addClass("wc-pv-separate-dial-code"),"checkout"==wcPvJson.currentPage){let e=n(`${wcPvJson.parentPage}`);e.on("checkout_place_order",function(){r(e)})}else if("account"==wcPvJson.currentPage){let e=n(`${wcPvJson.parentPage} form`);n(`${wcPvJson.parentPage}`).submit(function(){r(e)})}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment